Hey All,
I worked through a couple tutorials for Unity3D, but am still pretty new. I worked through one tutorial, where we create a bot with the camera behind it and you can move him forwards/ backwards and you use the mouse to move the camera, and thus his direction. Everything works great, except for when no input is given, he just moves backwards on his own.
//Walk Speed
var speed = 6.0;
//Gravity for the player
var gravity = 20.0;
//This will store the player movement as X Y Z, so we register it as a Vector3
private var moveDirection = Vector3.zero;
function FixedUpdate() {
//Register the controller variable with the character controller attached as a component to our player robot
var controller : CharacterController = GetComponent(CharacterController);
//Don't move the character in mid-air because it's not super mario :D
if (controller.isGrounded && Health.alive) {
// We are grounded, so recalculate
// move direction directly from axes
//We just calculate the movement in Z/Vertical axis
//You can also add strafe but our robot model doesn't have a strafe animation so...
moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
//We multiply the outcome with our speed variable, so it's customizable in the editor now
moveDirection *= speed;
}
//Rotate the player in Y axis based on the mouse X axis
if(Health.alive)
{
var rotateY = (Input.GetAxis("Mouse X") * 200 ) * Time.deltaTime;
controller.transform.Rotate( 0, rotateY, 0);
}
// Apply gravity
//It's not mandatory, because I'm not adding jump to this project yet, and the scene has nowhere to fall, but let's just keep it for now
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
//And finally we add the movement that we calculated earlier
if(Health.alive)
controller.Move(moveDirection * Time.deltaTime);
}
I’ve run some Debug lines and it seems that the z component is always near -1 if no keys are pressed… Also, I tried seeing what commenting out certain lines would do, and it seems to be with the gravity line. If I comment that out, the moving backwards stops. However, the bot no longer moves…
I’ve tried Googling and searching through here (where I found a question that seemed to be mine exactly from the header, but the link wouldn’t open…). I’m hoping I’m just missing something simple. Any and all help is greatly appreciated!
Dan