• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nourero · Jan 01, 2013 at 10:58 AM · charactercontrollerrotate

Character Rotation using CharacterController

Hello, I need to have my character rotate using character controller. I tried almost everything but nothing really works properly. There is no help that I can find that makes my search easier.

This is part of my script.

 void Update () {
         
     if(IsControlled){
         
         if(MoveDir.x>0){transform.localScale = MarioSize;}
             
         if(CC.isGrounded){
                 MoveDir = new Vector3 (0,0,Input.GetAxis("Vertical")*Speed*ExtPwr);
                 MoveDir.y = 0;

PLease answer with C# because that's what I need.

And for those who wonder yes its a Mario game. Thanks

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jan 01, 2013 at 12:18 PM

Where's your rotation code? And what's MoveDir?
Anyway, the CharacterController is a special capsule collider that is always aligned to the world Y axis. You can use transform.Rotate to rotate the character, but its capsule collider isn't affected. If you want to rotate only in the horizontal plane only (about Y axis), you can use this:

 public float rotSpeed = 90; // rotate speed in degrees/second
 
 void Update () {
     if(IsControlled){
        transform.Rotate(0, Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime, 0);
        if(MoveDir.x>0){transform.localScale = MarioSize;}
        ...

This rotates the character in the horizontal plane with the "Horizontal" axis.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nourero · Jan 01, 2013 at 02:11 PM 0
Share

I don't have one yet this is my problem. I have tried like 5 different ways and nothing worked how i wanted it.

$$anonymous$$oveDir is only $$anonymous$$oveDirection.

The script part worked good but i need my char to move the way he is facing. Like when i press "D" in my case he will rotate right and move in a circular motion. If you understand OFC.

Thanks for fast reply.

avatar image
1

Answer by AeonIxion · Jan 08, 2013 at 09:47 AM

Hi, I added this to my project a few days ago. Might not be the best way to solve it, but it works.

         if(Input.GetAxis("Horizontal") > 0)
             model.transform.localRotation =  Quaternion.Euler(new Vector3(0, 90, 0));
         else if(Input.GetAxis("Horizontal") < 0)
             model.transform.localRotation = Quaternion.Euler(new Vector3(0, 270, 0));

I'm using model.transform, because the script is attached to an object that is NOT the playermodel. If in your case it is the same object, you can just use transform.localRotation instead of model.transform.localRotation.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Piflik · Jan 01, 2013 at 03:21 PM

I am using a characterController with my player currently, and this is how my movement code looks:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
 
     public int groundSpeed = 5;
     public int airSpeed = 3;
     public int jumpHeight = 1;
 
     private Vector3 moveDirection = Vector3.zero;
     private float gravity = 20.0f;


     
     void Update () {
         CharacterController controller = GetComponent<CharacterController>();
         
 
         if(controller.isGrounded) {
             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
             moveDirection *= groundSpeed;
                 
             if(Input.GetButton("Jump")) {
                 moveDirection.y = jumpHeight;
                 }
             }
             
             else if(!controller.isGrounded && Mathf.Abs(moveDirection.x) < airSpeed) {
                 moveDirection.x = Input.GetAxis("Horizontal") * airSpeed;
             }

             //rotating here
             transform.right = Vector3.Slerp(transform.right, Vector3.right * Input.GetAxis("Horizontal"), 0.1f);
             
         }
         
         
         moveDirection.y -= gravity * Time.deltaTime;
             
         moveDirection.z = 0;
             
         controller.Move(moveDirection * Time.deltaTime);
     }
 
 }

It is 2.5D, so 2D gameplay on 3D objects. I removed some of the project specific code that would only serve to confuse you, so I am not entirely sure it is bug free.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nourero · Jan 01, 2013 at 04:16 PM 0
Share

Wow this looked complicated. Hehe I'm still learning how to scripts so this is something i'm not used too. I can say in short that i only need to have the character rotation. Like when i walk and turn around he will walk in a circular way.

Because my $$anonymous$$cher gave me this in school it was 2D work and we are going to translate and make it 3D haha. easy no thanks xD

Thanks for your help

avatar image
0

Answer by mayankela21 · Jun 07, 2020 at 10:43 AM

https://answers.unity.com/questions/967574/rotate-a-character-controller-in-the-direction-of.html?childToView=1739268#answer-1739268

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

rotate around character 1 Answer

rotate around character 2 Answers

Rotating Object with Mouse Axis is very choppy 1 Answer

Character making an unneeded rotation when first rotating camera around before moving 1 Answer

Assertion failed on expression: 'IsNormalized (ray.GetDirection ())' 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges