Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 SynGameDev · Apr 19, 2020 at 09:59 PM · cameravector3charactercontrollerquaternion

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

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
4
Best Answer

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);

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 xploreygames · Sep 27, 2020 at 04:56 PM 0
Share

Thanks, been looking for this for a long time!

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.

Follow this Question

Answers Answers and Comments

226 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 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

Getting character to face direction of camera 2 Answers

CharacterController, Move toward mouse 1 Answer

Workaround for Quaternion.eulerAngles 360 Degree Limit? 1 Answer

Camera rotation over Player 1 Answer

Launching a Character controller using vectors 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