Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Turning Characters to face moving direction - Quaternion.Lerp help

Discussion in 'Scripting' started by Vire, Apr 19, 2013.

  1. Vire

    Vire

    Joined:
    Jun 17, 2012
    Posts:
    178
    Hi, I have a script for a character controller. Currently it works but when I let go of a key it sets the rotation back to 0, which considering my script it SHOULD do. I don't know what to put there instead.

    Code (csharp):
    1. Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), >>>HERE<<<, Input.GetAxis("Vertical"));
    Because having that means if I put 0f in the >>>HERE<<< section then it will set rotateY back to 0 each update. What do I put there that will retain it's rotation on Y? I have tried so many things.

    Here is the full code. Please take a look, thanks very much.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TP_Controller : MonoBehaviour
    5. {
    6.     public float runSpeed = 5f;
    7.     public float turnSpeed = 20f;
    8.     public float gravity = 21f;
    9.     public float jumpSpeed = 12f;
    10.    
    11.     private Vector3 moveVector = Vector3.zero;
    12.    
    13.     public CharacterController CharacterController;
    14.    
    15.     void Awake()
    16.     {
    17.         CharacterController = gameObject.GetComponent("CharacterController") as CharacterController;
    18.     }
    19.    
    20.    
    21.     void Update()
    22.     {
    23.         ProcessMovement();
    24.     }
    25.    
    26.    
    27.     void ProcessMovement()
    28.     {
    29.         moveVector = new Vector3(Input.GetAxis("Horizontal"), moveVector.y, Input.GetAxis("Vertical"));
    30.        
    31.         if(CharacterController.isGrounded  Input.GetButton("Jump"))
    32.         {
    33.             moveVector.y = jumpSpeed;
    34.         }
    35.        
    36.         ProcessGravity();
    37.        
    38.         Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), Quaternion.identity.y, Input.GetAxis("Vertical"));
    39.         Quaternion lookRotation = Quaternion.LookRotation(currentRotation);
    40.        
    41.         transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime);
    42.        
    43.        
    44.        
    45.         CharacterController.Move(moveVector * Time.deltaTime);
    46.     }
    47.    
    48.     void ProcessGravity()
    49.     {
    50.         if(!CharacterController.isGrounded)
    51.         {
    52.             moveVector.y -= gravity * Time.deltaTime;
    53.         }
    54.     }
    55.    
    56. }
    57.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    transform.eulerAngles.y

    I think... the y component of the vector3 representation of the transform's current orientation :)
     
  3. Vire

    Vire

    Joined:
    Jun 17, 2012
    Posts:
    178
    That's what I thought, but using it just causes weird rotations and it still just flips back to 0 on y.
     
  4. Pmyl

    Pmyl

    Joined:
    Apr 8, 2013
    Posts:
    11
    transform.rotation.y instead of Quaternion.identity.y? I don't know...
     
  5. Vire

    Vire

    Joined:
    Jun 17, 2012
    Posts:
    178
    Same thing.

    If I think about it some more it's not SETTING the rotation.y to 0f, it's not affecting the rotation.y at all because it's 0f. Which means it SHOULDNT rotate to face the front each time... but it is.

    So why would this cause the player to rotate forwards each time?

    Code (csharp):
    1. Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
    2. Quaternion lookRotation = Quaternion.LookRotation(currentRotation, Vector3.up);
    3.  
    4. transform.rotation = Quaternion.Lerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime);
     
  6. Vire

    Vire

    Joined:
    Jun 17, 2012
    Posts:
    178
    Doing this helped a bit, but still when I rotate it snaps a little into another direction...

    Code (csharp):
    1. Vector3 currentRotation = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
    2.  
    3. if(currentRotation != Vector3.zero)
    4. {
    5.     Quaternion lookRotation = Quaternion.LookRotation(currentRotation, Vector3.up);
    6.  
    7.     transform.rotation = Quaternion.Lerp(transform.rotation, lookRotation, turnSpeed * Time.deltaTime);
    8. }
     
  7. jarden

    jarden

    Joined:
    Mar 29, 2011
    Posts:
    74
    One thing I noticed is in the LookRotation function your suposed to use direction vectors not rotations. Maybe try Quaternion.Euler(currentRotation) instead. Or maybe transform.Rotate(currentRotation,Space.Self).
     
  8. Vire

    Vire

    Joined:
    Jun 17, 2012
    Posts:
    178
    Thanks :) Not having luck with either of those, though. Quaternion.Euler(currentRotation) causes it to not even rotate.

    transform.Rotate not sure how to use that properly.

    Vector3 finalRotation = transform.Rotate(currentRotation, Space.Self);

    = Cannot implicitly convert type `void' to `UnityEngine.Vector3'
     
  9. jarden

    jarden

    Joined:
    Mar 29, 2011
    Posts:
    74
    transform.Rotate rotates the object the script is on unless you use something like object2.transform.Rotate.
     
  10. Vire

    Vire

    Joined:
    Jun 17, 2012
    Posts:
    178
    I can't use that with a quaternion, and since I need lerp to smooth the turning it wont work.