- Home /
Rotate Character with movement
Right, I have a simple movement script. I haven't picked up unity in a while and for some reason it seems i have forgotten some of the basics. I want my character to face the direction he is moving. Here is the script i have for movement at the moment.
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero;
function Update() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
Answer by syclamoth · Oct 09, 2011 at 10:21 AM
Like this-
if(moveDirection.magnitude > 0.05f)
{
transform.LookAt(transform.position + moveDirection);
}
I mean, that's just one way- you can do it any way really.
Oh yeah- you have to do it before any of the jump stuff gets added.
Answer by TheFrankman123 · Oct 12, 2011 at 11:55 PM
Turns out it's just easier to use the third person controller script in the standard assets.
Hi,
I am having same kind of trouble, but ins$$anonymous$$d of moving all direction , my character is restricted to move in x-direction only -
moveDirection = new Vector3(0,0,Input.GetAxis("Horizontal"));
basically what i want is to move my character in 2d platform.
Your answer
![](https://koobas.hobune.stream/wayback/20220613002132im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Two Keys ws or wd detection 3 Answers
How can I stop player snapping back to 0,0,0 rotation when I release keys? 1 Answer
Character Rotate Properly 2 Answers
Original Blender character faces ground when not walking 2 Answers
Problem with Movement Script 0 Answers