- Home /
Character Controller Rotation
So, in my game, I created a character, and I want to use "Character controller" to move it. It is moving fine, but I can't rotate it. Actually, I am being able to rotate it using transform.Translate, but that doesn't affect the way I move my character with Character Controller, so it always moves foward, backward, left or right whithout taking in consideration it's roation. Basicly, even though I rotate the character around, it move around as if it wasn't rotating. Sorry if this is an obvious and stupid question, but I am very new to Unity and scripting. Thanks!
Answer by aldonaletto · Oct 08, 2011 at 10:02 PM
You should not move with Translate, since collisions are not detected - you should use CharacterController.Move instead.
This is a simple script that I've adapted from the CharacterController.Move example to rotate instead of just go right and left. It's very simple, and you can modify it easily if you want to add new features:
var speed : float = 6.0; var jumpSpeed : float = 8.0; var turnSpeed : float = 60; var gravity : float = 20.0;
private var moveDirection : Vector3 = Vector3.zero; private var controller: CharacterController;
function Start(){ controller : CharacterController = GetComponent(CharacterController); }
function Update() { var turn: float = Input.GetAxis("Horizontal"); transform.Rotate(0, turn turnSpeed Time.deltaTime, 0); if (controller.isGrounded) { moveDirection = transform.forward Input.GetAxis("Vertical") 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 unity_QjPE1Ur0mLEAlA · Jun 05, 2019 at 09:28 PM
Ok this post is realy old, but I found a solution to rotate your character Controller with translate!
All you need to do is to rotate your object with translate:
transform.eulerAngles = new Vector3(0, yourRotation, 0);
and Apply to your Move direction the "new Transformed Direction" with:
moveDirection = transform.TransformDirection(moveDirection);
at the end you apply the moveDirection to your Character controller:
controller.Move(moveDirection * Time.deltaTime);
Answer by Sord · Feb 20, 2013 at 11:22 AM
Also this is NOT working:
transform.rotation.SetLookRotation(moveDirection);
Insted use like this:
Quaternion rot = new Quaternion();
rot.SetLookRotation(moveDirection);
transform.rotation = rot;
Thanks for your code. I find it cannot lock the rotation Direction when moving; if you release the move direction, the rotation direction will be reset to the original.
Answer by dweitkunat · Oct 09, 2011 at 12:10 AM
You should make sure you're passing Space.self as the second argument to transform.Translate, otherwise the object will move in world space, regardless of rotation.
Always remember there are two coordinate systems to deal with: Global and Local.
Global axis directions never change, but local axis directions change with rotation.
Your answer
Follow this Question
Related Questions
How to fix a infinity jump? 1 Answer
Face Direction Movement using CharacterController 1 Answer
fps script not working 1 Answer
rotate player towards the mous 1 Answer
Help with my Character Controller 1 Answer