- Home /
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);
}
}
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;
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) {
...
}
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!
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);
}
}
}
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!