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 /
  • Help Room /
avatar image
0
Question by BarbaricSpaceWhale · Sep 30, 2015 at 10:16 AM · rotationcontroller

3D Character Rotation Issue

Hello!

I've been working on 3D character controls for my game, and I've managed to make a character that takes the forward direction of the camera and the input on the vertical and horizontal axes, moves in the combined direction and rotates to face the way it's moving! It works a lot like a character in a 3D platformer and feels very nice. The character uses a rigidbody for the sake of controls, and has no Character Controller component or animations.

The problem I'm getting however, is that whenever the character isn't being moved with WASD or the gamepad analog sticks, the character immediately faces directly north in the game world and logs the error:


Look rotation viewing vector is zero

UnityEngine.Quaternion:LookRotation(Vector3, Vector3)

PlayerController3:Movement() (at Assets/Scripts/PlayerController3.cs:78)

PlayerController3:FixedUpdate() (at Assets/Scripts/PlayerController3.cs:53)


Inside the code are two methods called "Movement" and "CameraForward" and I think these are where the problem lies. I feel that I don't understand quaternions very well and it is probably because I'm misusing one. What I want is the character to face whatever direction it was moving before it stopped, and to not get that error... .

If any other information is needed, let me know.

 [SerializeField]
     public float movePower = 5f;
     [SerializeField]
     public float jumpPower = 325f;
 
     public float groundRayLength = 1f;
     private Rigidbody rBody;
     private bool jump;
     private Transform cam;
     private Vector3 camForward;
     float horizontalMove;
     float verticalMove;
     private Vector3 move;
 
     // Use this for initialization
     private void Awake()
     {
         if (Camera.main != null)
         {
             cam = Camera.main.transform;
         }
         else
         {
             Debug.LogWarning("Warning: no main camera found. Player needs a Camera tagged \"MainCamera\", for camera-relative controls.");
             // we use world-relative controls in this case, which may not be what the user wants, but hey, we warned them!
         }
     }
 
     void Start()
     {
         rBody = GetComponent<Rigidbody>();
     }
 
     void GetControls()
     {
         horizontalMove = Input.GetAxis("Horizontal");
         verticalMove = Input.GetAxis("Vertical");
         jump = Input.GetButton("Jump");
     }
 
     // Update is called once per frame
     public void FixedUpdate()
     {
         GetControls();
         CameraForward();
         Movement();
         Jumping();
     }
 
     void CameraForward()
     {
         if (cam != null)
         {
             // calculate camera relative direction to move:
             camForward = Vector3.Scale(cam.forward, new Vector3(1, 0, 1)).normalized;
             move = (verticalMove * camForward + horizontalMove * cam.right) * movePower * Time.deltaTime;
         }
         else
         {
             // we use world-relative directions in the case of no main camera
             camForward = Vector3.forward;
         }
     }
 
     void Movement()
     {
         rBody.MovePosition(transform.position + move * Time.deltaTime);
 
         if (move != null)
         {
             rBody.MoveRotation(Quaternion.LookRotation(move, Vector3.up));
         }
         else
         {
             rBody.MoveRotation(Quaternion.LookRotation(camForward, Vector3.up));
         }
     }
 
     void Jumping()
     {
         Debug.DrawRay(transform.position, -Vector3.up * groundRayLength, Color.red);
 
         //If the player's center is less than the groundRayLength distance from the ground, they can jump 
         if (Physics.Raycast(transform.position, Vector3.down, groundRayLength) && jump)
         {
             // change upwards velocity directly.
             rBody.velocity = new Vector3(0, 1, 0) * jumpPower * 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

0 Replies

· Add your reply
  • Sort: 

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

30 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

Related Questions

Sprite gets rotated when Joystick Y at 1 or -1? 0 Answers

Alternatives for Input.GetButton? 1 Answer

How to mix 3 player controller scripts for a 3D isometric game 0 Answers

How to set a max rotation angle 1 Answer

Some problems with handmade basic player controller 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