Apply this to your game object to look in the direction of your movementVector:
transform.LookAt(transform.position + movementVector);
EDIT: (I first responded in a comment then realized I should’ve edited my answer)
Your sample code helps us understand what you want to accomplish a bit better. I think there are a couple of reasons you’re not seeing what you want to see with your current code.
One issue I see is that you’re moving your CharacterController in a relative direction (using the direction = transform.TransformDirection(direction)), but rotating with the world absolute of Vector3(cordX, 0, cordZ), so the two won’t match.
And also, since your relative direction changes every single frame based on your new forward direction, your movement direction will probably just be spinning around your current location since, say, moving to the right relative to where you are will make you go in a tiny circle.
The error you see about the look rotation viewing vector being 0 is that you’re not checking for your direction vector to be (0,0,0), which will cause that error.
If you want the relative movement, you might want to use a damping so that you won’t spin in place every frame. I took your code and made a few modifications. Not sure if this is what you want, and I don’t know what else you have attached to this game object besides your CharacterController, but the following might do something like you want?
using UnityEngine;
using System.Collections;
public class CCTest : MonoBehaviour {
public float rotateDegreesPerSecond = 180.0f;
private Vector3 direction = Vector3.zero;
public float moveSpeed = 10.0f;
public float gravity = 10.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CharacterController CharacterMove = GetComponent<CharacterController>();
float cordX = Input.GetAxis("Horizontal");
float cordZ = Input.GetAxis("Vertical");
if (CharacterMove.isGrounded) {
direction = new Vector3(cordX, 0, cordZ);
direction = transform.TransformDirection(direction);
}
// If direction is not zero, rotate towards direction (via rotateDegreesPerSecond) and move CharacterController in current forward vector
if(direction != Vector3.zero)
{
// Take care of direction being directly behind our current forward vector since the
// Quaternion.RotateTowards will continuously alternate between right and left rotations to get there,
// resulting in never getting there, just wiggling around the current transform.rotation.
if(Vector3.Angle(transform.forward, direction) > 179)
{
// This will cause us to always turn to the right to go the opposite direction
direction = transform.TransformDirection(new Vector3(.01f, 0, -1));
}
transform.rotation = Quaternion.RotateTowards( transform.rotation, Quaternion.LookRotation(direction), rotateDegreesPerSecond * Time.deltaTime);
Vector3 moveDirection = transform.forward;
moveDirection.y -= gravity * Time.deltaTime;
CharacterMove.Move(moveDirection * moveSpeed * Time.deltaTime);
}
}
}
Jarz, beginners should really just use "LookAt" rather than touching quaternions for any reason
– FattieThat's great if you want the character to look in one direction :P
– BadSeedProductionsThank You So Much You Saved My Life :')
– Wicaodian