Quantcast
Channel: Questions in topic: "maintain"
Viewing all articles
Browse latest Browse all 19

How to make the Character stay where he is facing?

$
0
0
I know this question was asked thousands of times. I've searched and used a lot of codes to make the character face where he is moving. It's originally in C++ code so I convert it into JS code (I'm not used to C++). I found this code here by Kanox and I think it is almost perfect. Here's the code I'm currently using: var moveSpeed : float = 8.0; var rotationSpeed : float = 10.0; private var moveDirection : Vector3 = Vector3.zero; private var previousLocation : Vector3 = Vector3.zero; function Update() { var controller : CharacterController = GetComponent.(); if (controller.isGrounded) { moveDirection = Vector3.zero; previousLocation = transform.position; if(Input.GetKey(KeyCode.W)) { moveDirection.z = 1; } if(Input.GetKey(KeyCode.S)) { moveDirection.z = -1; } if(Input.GetKey(KeyCode.A)) { moveDirection.x = -1; } if(Input.GetKey(KeyCode.D)) { moveDirection.x = 1; } transform.position = Vector3.Lerp(transform.position, transform.position + moveDirection.normalized, Time.fixedDeltaTime * moveSpeed); transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed); } The problem here is the character resets where he is facing. I hope you fix/answer my problem.

Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>