- Home /
Object not moving in forward direction
Hi Guys
I'm have a movement script and a camera script. the movement script is self explanatory. it uses a character controller to move the object. the camera is used to rotate the camera when the object isn't moving & rotate the camera & the object when it is.
The object rotates fine, but when the object moves, it only moves along the axis and not in the forward direction
CameraController
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraController : MonoBehaviour
 {
 
     [Header("Camera Settings")]
     [SerializeField] private float _RotationSpeed;
     private Transform _Target;
     private Transform _Player;
 
     [Header("Mouse Settings")]
     private float _MouseX;
     private float _MouseY;
 
     private void Awake() {
         // Get the Objects
         _Player = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
         _Target = GameObject.FindGameObjectWithTag("CameraTarget").GetComponent<Transform>();
 
     }
 
     private void LateUpdate() {
         CameraControl();
     }
 
     private void CameraControl() {
         // Get the mouse settings
         _MouseX += Input.GetAxis("Mouse X") * _RotationSpeed;
         _MouseY += Input.GetAxis("Mouse Y") * _RotationSpeed;
 
         _MouseY = Mathf.Clamp(_MouseY, -35, 60);                // Clamp the rotation so we don't rotate to far
 
         transform.LookAt(_Target);                          // look at the target
 
         _Target.rotation = Quaternion.Euler(_MouseY, _MouseX, 0);           // Rotate the target making the cam rotate
 
         // If the player is moving, than rotate the player  
         if(_Player.GetComponent<PlayerMovement>().CheckIfMoving()) { 
             _Player.rotation = Quaternion.Euler(0, _MouseX, 0);
         }
     }
     
 }
PlayerMovement
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 [RequireComponent(typeof(BaseCharacter))]
 [RequireComponent(typeof(Animator))]
 [RequireComponent(typeof(CharacterController))]
 public class PlayerMovement : MonoBehaviour
 {
     [Header("Movement Speeds")]
     [SerializeField] private float CurrentMovementSpeed = 0f;          // Set the current movement speed
     [SerializeField] private float StandardMovementSpeed = 5.0f;            // Standard Movement Speed
     [SerializeField] private float RunningMovementSpeed = 10.0f;            // Running Movement Speed
 
 
     [Header("Gravity")]
     private Vector2 _Velocity;                              // Velocity Direction
     [SerializeField] private float _Gravity;                                 // Gravity 
     [SerializeField] private float _JumpForce;
 
     [Header("Movement Status")]
     private bool _IsMoving;
 
 
     private Vector3  _PreviousPosition;
 
 
     [Header("Objects")]
     private Animator _Animator;
     private CharacterController _CharacterController;
     private BaseCharacter _Player;
     private CameraController _Camera;
 
 
 
     private void Awake() {
         GetObjects();
     }
 
 
     private void Update() {
         Move();
         Jump();
     }
 
     private void Move() {
 
         // BUG # 1: Forward Direction not being set
 
         Vector3 MoveDir = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));           // Get the input details
         _CharacterController.Move(MoveDir * Time.deltaTime * 5f);               // Move the character controll as needed
 
 
         // TODO: Toggle Movement Animations  
 
         if(_PreviousPosition != transform.position) {
             _IsMoving = true;
         } else {
             _IsMoving = false;
         }
 
         Gravity();
     }
     
     private float GetMoveSpeed() {
 
         // TODO: Implement Function
         return StandardMovementSpeed;
     }
 
     public bool CheckIfMoving() {
         if(_IsMoving)
             return true;
         else
             return false;
     }
 
     private void Gravity() {
         _Velocity.y += _Gravity * Time.deltaTime;                   // Set the gravity
         _CharacterController.Move(_Velocity * Time.deltaTime);          // Set the movemenet           
 
         // Check if gravity is required to be applied
         if(_CharacterController.isGrounded && _Velocity.y < 0)
             _Velocity.y = 0f;
 
         
     }
 
     private void Jump() {
         if(Input.GetKeyDown(KeyCode.Space) && _CharacterController.isGrounded) {
             _Velocity.y += Mathf.Sqrt(_JumpForce * -2 * _Gravity);      
         }
     }
 
     // TODO: Implement Sprinting & Stamina Control
 
     private void GetObjects() {
         _Animator = GetComponent<Animator>();
         _CharacterController = GetComponentInChildren<CharacterController>();
         _Player = GetComponent<BaseCharacter>();
         _Camera = GetComponent<CameraController>();
     }
 
 }
 
Can anyone see what the issue is?
Thanks
Answer by jkpenner · Apr 20, 2020 at 02:33 AM
The character controller's move method moves the character based a vector in world space and is not affected by the character's rotation. You will need to transform the input from the character's local space into world space. You should be able to update your move code to the following:
 // Create a vector based on the user's input.
 Vector3 input = new Vector3(
     Input.GetAxis("Horizontal"), 
     0f, 
     Input.GetAxis("Vertical")
 );
 
 // Transfrom the user input from character's local space
 // into a world space vector.
 Vector3 MoveDir = this.transform.TransformDirection(input);
 
 // Move character based on world space vector.
 _CharacterController.Move(MoveDir * Time.deltaTime * 5f);
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                