• 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 Ochreous · May 28, 2016 at 11:52 PM · c#rotationgameobjecttransformcharactercontroller

CharacterController.Move Not Corresponding to gameobject.transform.rotation

I'm trying to create my own FPS controller and I'm using CharacterController.Move() to move. However it isn't corresponding to the gameobject's transform.rotation like I was expecting since I've assigned the gameobject's transform.rotation = Quaternion.Euler(rot). The characterController moves but only along Input.GetAxis("Horizontal") and Input.GetAxis("Vertical"). Does CharacterController.Move() operate independently from a gameobject's transform.rotation? That's what I think is happening. How would I be able to get the CharacterController to move while also following the gameobject's transform.rotation?

 using UnityEngine;
 using System.Collections;
 
 public class FirstPersonController : MonoBehaviour {
     
     public Vector2 rot;
     public Vector3 movement;
     
     public float movementSpeed = 5.0f;
     public float mouseSensitivity = 5.0f;
     public float jumpAmount = 5.0f;
     
     public float clampAngle = 90.0f;
     
     public CharacterController characterController;
     
     // Update is called once per frame
     void Update () {
         // Rotation
         rot.y += Input.GetAxis("Mouse X") * mouseSensitivity;
         rot.x += -Input.GetAxis("Mouse Y") * mouseSensitivity;
         rot.x = Mathf.Clamp(rot.x, -clampAngle, clampAngle);
         transform.rotation = Quaternion.Euler(rot);
         
         movement.y += Physics.gravity.y * Time.deltaTime;
         
         if( characterController.isGrounded && Input.GetButton("Jump") ) {
             movement.y = jumpAmount;
         }
         
         movement = new Vector3(Input.GetAxis("Horizontal") * movementSpeed, movement.y, Input.GetAxis("Vertical") * movementSpeed);
         
         characterController.Move(movement * Time.deltaTime );
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ninja_gear · May 28, 2016 at 11:59 PM

The .Move() method takes absolute movement, so you need to rotate the input by your transform's rotation before attempting to move.

          movement = movement * transform.rotation;
          characterController.Move(movement * Time.deltaTime );
Comment
Add comment · Show 4 · 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 Ochreous · May 29, 2016 at 12:46 AM 0
Share

Okay so now my character is moving backwards on it's own without any keyboard input. I assume this is due to characterController.$$anonymous$$ove now including transform.rotation for it's values along with keyboard inputs. How do I include only the keyboard inputs for movement but also have characterController.$$anonymous$$ove correspond with the gameobject's transform.rotation?

         movement = transform.rotation * movement;
         characterController.$$anonymous$$ove(movement * Time.deltaTime );

avatar image ninja_gear Ochreous · May 29, 2016 at 01:07 AM 0
Share

edit:

 if (movement.magnatude != Vector3.zero)
 {
   movemovement = transform.rotation * movement;
   characterController.$$anonymous$$ove(movement * Time.deltaTime );
 }
avatar image Ochreous ninja_gear · May 29, 2016 at 02:40 AM 0
Share

The charactercontroller is still moving with the mouse.

          if (movement.magnitude != 0)
          {
            movement = transform.rotation * movement;
            characterController.$$anonymous$$ove(movement * Time.deltaTime);
          }
Show more comments

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

165 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Transform a gameobject y rotation to another gameobject y rotation 1 Answer

C# 2d Always Rotate Facing Gameobject 1 Answer

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers

Flip over an object (smooth transition) 3 Answers

Auto leveling help 0 Answers


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