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 Puckle33 · Apr 16, 2018 at 11:15 PM · c#charactercontrollerquaternionlookrotation

My LookRotation forces my character to snap back, and face starting direction after I stop moving?

using System.Collections; using System.Collections.Generic; using UnityEngine;

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

 public float speed = 6.0F;
 public float jumpSpeed = 8.0F;
 public float gravity = 20.0F;
 private Vector3 moveDirection = Vector3.zero;

 private CharacterController controller;

 private void Awake()
 {
     controller = GetComponent<CharacterController>();
 }

 void Update()
 {
     if (controller.isGrounded)
     {
         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

         Quaternion rotation = Quaternion.LookRotation(moveDirection, Vector3.up);
         transform.rotation = rotation;

         moveDirection *= speed;
         if (Input.GetButton("Jump"))
         {
             moveDirection.y = jumpSpeed;
         }

         Debug.DrawLine(transform.position, moveDirection);

     }
     moveDirection.y -= gravity * Time.deltaTime;
     controller.Move(moveDirection * Time.deltaTime);
 }

}

Comment
Add comment · Show 3
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 bobisgod234 · Apr 16, 2018 at 11:36 PM 2
Share

With Horizontal and vertical axes 0 (i.e. no input), you try to compute the direction of a vector that has no direction (all components are 0). Try changing this:

 transform.rotation = rotation;

To this:

 if (moveDirection != Vector3.zero)
     transform.rotation = rotation;
avatar image Kishotta bobisgod234 · Apr 17, 2018 at 12:59 AM 1
Share

To add to this solution: when releasing your inputs, the movement vector might gradually become zero rather than snap to 0 meaning the camera could still start looking towards 0 even if it doesn't get all the way there. You can fix that with:

 if (moveDirection.magnitude > cameraDeadzone) {
     ...
 }
avatar image Puckle33 bobisgod234 · Apr 17, 2018 at 02:26 AM 0
Share

Thank Y'all so much this was perfect! I have my character model due tomorrow and now he is looking great, saved my butt. :D Thank you so much!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MarioSantoso · Apr 17, 2018 at 02:07 AM

Well, your code is a bit confusing as I'm not sure whether you are trying to achieve movement (moving) or rotation (turning), so I will give you an example of both and let you modify as you see fit.

Assign the following script (name it MoveDirection.cs) to an object and play

 using UnityEngine;
 
 public class MoveDirection : MonoBehaviour
 {
     public float turnRate;
     public float moveSpeed;
 
     public bool move;
 
     private void Update()
     {
 
         // Use spacebar to switch between moving and turning.
         if (Input.GetKeyDown(KeyCode.Space))
             move = !move;
 
         Vector2 _getAxis = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
 
         if (move)
         {
             Debug.Log("moving");
             transform.Translate(
               _getAxis.x * moveSpeed * Time.deltaTime,
               0,
               _getAxis.y * moveSpeed * Time.deltaTime,
               Space.Self);
         }
         else
         {
             Debug.Log("turning");
             transform.Rotate(
                 _getAxis.x * turnRate * Time.deltaTime,
                 _getAxis.y * turnRate * Time.deltaTime,
                 0,
                 Space.Self);
         }
     }
 }
 
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 Lylek · Apr 17, 2018 at 04:04 AM

Well, you're rotating based on Input.GetAxis. If you stop moving, your input axis is 0, which will default you back to the same rotation each time. Check whether or not the appropriate input is provided, then if so, rotate accordingly (or otherwise, do nothing).

Hope that helps!

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.

Follow this Question

Answers Answers and Comments

93 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

Related Questions

Distribute terrain in zones 3 Answers

Player Control. Roller Ball with Cube Acceleration. 1 Answer

Character movement always faces forwards 1 Answer

Resetting rotation with Quaternion.Euler 1 Answer

Quaternion LookRotation problems while moving up hill. 2 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