Daniel Roca Lopez
Daniel Roca Lopez

Reputation: 574

unity player moves too fast even with speed 0.000000001f

I have the following script attached to my ball on a game:

public class MovePlayer : MonoBehaviour {
    //public GameObject packman;
    // Use this for initialization
    private Vector3 currentSpeed;
    void Start () {
        currentSpeed = new Vector3(0.0f, 0.0f, 0.0f);
    }

    // Update is called once per frame
    void Update () {

        if (Input.GetKey(KeyCode.LeftArrow)){
            currentSpeed.x = -(0.0001f);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            currentSpeed.x = 0.0001f;
        }
        else currentSpeed.x = 0;

        /*if (Input.GetKeyDown(KeyCode.UpArrow))
        {
        }*/

        //move packman
        this.transform.Translate(Time.deltaTime * currentSpeed.x, Time.deltaTime * currentSpeed.y,
            Time.deltaTime * currentSpeed.z);
    }
}

Then I touch left or right arrow on the game, and the ball moves really fast to one direction and never stops even if I touch the other arrow.

Upvotes: 1

Views: 2773

Answers (2)

N Z
N Z

Reputation: 1

use FixedUpdate() like this

    public class MovePlayer : MonoBehaviour {
    //public GameObject packman;
    // Use this for initialization
    private Vector3 currentSpeed;
    void Start () {
        currentSpeed = new Vector3(0.0f, 0.0f, 0.0f);
    }

    // Update is called once every 1/60th second
    void FixedUpdate () {

        if (Input.GetKey(KeyCode.LeftArrow)){
            currentSpeed.x = -(0.0001f);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            currentSpeed.x = 0.0001f;
        }
        else currentSpeed.x = 0;

        /*if (Input.GetKeyDown(KeyCode.UpArrow))
        {
        }*/

        //move packman
        this.transform.Translate(Time.deltaTime * currentSpeed.x, Time.deltaTime * 
        currentSpeed.y, Time.deltaTime * currentSpeed.z);
    }
}

Upvotes: 0

NebulaGrey
NebulaGrey

Reputation: 41

I found it is because I added "Physics -> character controller" to the ball. Removing this component did the job. Why was character controller producing the described effect? – Daniel Roca Lopez

It sounds like the Character Controller you had added accidentally, had pre-written values for how the Object behaved.
So on top of your MovePlayer script, you also got the Movement from CharacterController.

Upvotes: 1

Related Questions