- Home /
Help with my Character Controller
Hey, I am struggling to find a way to make the player rotate to its current direction. For example if I press the down arrow, the player would face the camera and walk towards it. Or if I pressed right the player would face and walk right. Here is my current script :
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) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
I have tried many methods but simply can't find a way to do it, any help would be appreciated.
can't test the code, but I'd suggest to get the camera objects forward vector (transform.forward) and use that to figure out which way the character should face
Answer by gardian06 · Sep 28, 2013 at 06:31 PM
So if I am understanding your context correctly then you are simply wanting the transform.forward to become equal to either Vector3.forward, Vector3.back, Vector3.left, or Vector3.right depending on if the player has pressed 1, or 4 buttons, and probably not blending these together (if you do want to blend then then things get a little harder), and then move the character in that direction.
essentially I am seeing:
if(Input.GetAxis("Vertical") < 0){
transform.forward = Vector3.back;
}else if(Input.GetAxis("Vertical") > 0){
transform.forward = Vector3.forward;
}else if(Input.GetAxis("Horizontal") < 0){
transform.forward = Vector3.left;
}else if(Input.GetAxis("Horizontal") > 0){
transform.forward = Vector3.right;
}
moveDirection = transform.forward;
// do gravity work here
controller.Move(moveDirection * Time.deltaTime); // I would suggest adding in some multiplication factor because Time.deltaTime is usually rather small (somewhere between .01, and maxTimestep)
the next suggestion would be to store the transform so that you don't have to keep looking it up for performance
do you mean that you want them all to be 45 degrees from what they are, or you also want the ability to go at 45 degree angles as well?
I mean if you press up then down, the player would smoothly go from forward to backward, and if you press right and up the player would turn to a 45 degree angle smoothly from the last position. Basically like a LEGO Game's controller. Which I imagine is very complicated.
I am still not clear on if you only want to limit this to 90 degree rotations, or you now want 45 degree rotations, but if you want smooth rotations then you will probably want to store a Vector3 (targetForward) set that as the desired forward direction, then either Vector3.Lerp, or Vector3.RotateToward the forward vector to the target vector.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Jaggie, uneven speed with basic movement script. 0 Answers
Look rotation viewing is zero warning 0 Answers
Camera relative movement 0 Answers
How do I keep my character facing the direction of travel after movement stops? 1 Answer