- Home /
Is it a bug?
Hello i have a problem with Unity 4.0.0 . When i play the AngryBots project or even when i make my own char with simple walking script it automatically walk with pressing any button. I try uninstall the unity, i try with older unity, i delete even from registry, still the same thing. Why, is it a bug, am i doing something wrong?
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection 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
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
I am using this script for all my walking chars and it works, but now its walking automatically.
Please help, and thank you!
Answer by Eugenius · Jul 10, 2013 at 03:37 PM
This happens because your script tells the character to move. You should edit the code like this
if(Input.GetKeyDown("x")){
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;}
This way, your character will move when you press X.
Your answer
Follow this Question
Related Questions
Missing Monobehavior after switched OS from Windows to Mac 1 Answer
Using a footstep C# script and keep getting an "IndexOutOfRangeException" 2 Answers
Crazy Perspective in Scene Editor 1 Answer
Bug with my enemyAI 2 Answers
Lighting Problem 2 Answers