Hello,
I’m trying to move my character using the Character Controller/Motor. So far, my character rotates wherever my mouse is, but I’m struggling to get the character to move in the direction/rotation of itself.
Is is the code that allows my character rotate is the mouses direction:
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var playerRotateSpeed = 10;
var hitdist = 0.0;
var targetPoint = ray.GetPoint(hitdist);
var lookPos = targetPoint - transform.position;
lookPos.y = 0;
var targetRotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
Now I’m trying to get the ‘motor.inputMoveDirection’ to move the character is its current direction. I know I could just use transform.Translate, but I really need to use the character controller/motor.
Thanks
Okay, so I figured it out.
You want to add the platform controller to your player, and change the platform script to this:
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var playerRotateSpeed = 10;
var hitdist = 0.0;
var targetPoint = ray.GetPoint(hitdist);
var lookPos = targetPoint - transform.position;
lookPos.y = 0;
var targetRotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, playerRotateSpeed * Time.deltaTime);
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (directionVector != Vector3.zero) {
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
directionLength = Mathf.Min(1, directionLength);
directionLength = directionLength * directionLength;
directionVector = directionVector * directionLength;
}
directionVector = transform.rotation * directionVector;
motor.inputMoveDirection = directionVector;