Hey guys, I’m trying to recreate the 2D Controller on my own (mainly because I want to fully understand why it does what it does). Unfortunately, my first few attempts failed miserably, so I grabbed the Controller.Move code from the docs.
Here’s what I have so far:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
function Awake(){
transform.TransformDirection (Vector3.forward);
//transform.Rotate(0, 90, 0);
}
function FixedUpdate() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
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);
animation.CrossFade("walk");
}
This works fine when I move the character – that is, he moves along the X-axis (either left or right) when I hit the arrow keys, and he jumps when I hit the jump key. The problem is that he won’t rotate to face whichever direction he’s moving in.
I’ve scanned the boards a bunch of times, Googled to see if any of the bloggers have addressed this, analyzed the PlayerPlatformController from the 2D tutorial, and tried various other things to try and make it simply turn around and face the direction in which it’s moving. Unfortunately, I still can’t get it.
I wanna say that I have to use some form of Quaternion.LookRotation(), but that’s not working, either. Any time I try and adjust the rotation, my model starts bouncing around or spinning around uncontrollably. I just want it to rotate and face the direction in which it’s moving.
Any suggestions? Any help and/or explanations would be much appreciated.
Thanks[/code]