BriannaXD
BriannaXD

Reputation: 169

Change the controls of a player after a collision?

So basically the player is moving all well and good. Until he hits an object. I want to change the players movement when he collides with a certain object. eg: Homer Simpson is running around but when he hits a duff his movement changes for a few seconds.

Here's my code so far:

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour {
    public float speed = 1.0f;
    public int score = 0;
    public bool gameOverRed = false;
    public float size = 1f;
    public float duffSpeed = 1.0f;
    private float timer;
    private bool hitDuff = false;
void start() {
    timer = 0.0f;
}
// Update is called once per frame 
void Update () {

    if (hitDuff) {
        timer += Time.deltaTime;

        if (timer > 5.0) {
            start ();
            duffMovement ();
            hitDuff = false;
        }
    } else {
        movement ();
    }

}

public void OnCollisionEnter2D(Collision2D coll)
{

    if(coll.gameObject.CompareTag("Cube")) {
        speed -= 2;
        score++;
        transform.localScale += new Vector3(size, size, 0);

    }

    if(coll.gameObject.CompareTag("Duff")){
        hitDuff = true;
    }

}

private void movement()
{

    if (Input.GetKey (KeyCode.D)) {
        transform.rigidbody2D.velocity = (new Vector2 (speed, 0));
    }
    if (Input.GetKey (KeyCode.A)) {
        transform.rigidbody2D.velocity = (new Vector2 (-speed, 0));

    }
    if (Input.GetKey (KeyCode.S)) {
        transform.rigidbody2D.velocity = (new Vector2 (0, -speed));
    }
    if (Input.GetKey (KeyCode.W)) {
        transform.rigidbody2D.velocity = (new Vector2 (0, speed));
    }
    if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.A)) {
        transform.rigidbody2D.velocity = (new Vector2 (-speed, -speed));
    }
    if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.D)) {
        transform.rigidbody2D.velocity = (new Vector2 (speed, -speed));
    }
    if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.D)) {
        transform.rigidbody2D.velocity = (new Vector2 (speed, speed));
    }
    if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.A)) {
        transform.rigidbody2D.velocity = (new Vector2 (-speed, speed));
    }
    if (speed <= 20) {
        speed = 20;
    }
}

    private void duffMovement() {

                if (Input.GetKey (KeyCode.D)) {
                transform.rigidbody2D.velocity = (new Vector2 (-speed, 0));
                }
                if (Input.GetKey (KeyCode.A)) {
                transform.rigidbody2D.velocity = (new Vector2 (speed, 0));  
                }
                if (Input.GetKey (KeyCode.S)) {
                transform.rigidbody2D.velocity = (new Vector2 (0, speed));
                }
                if (Input.GetKey (KeyCode.W)) {
                transform.rigidbody2D.velocity = (new Vector2 (0, -speed));
                }
                if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.A)) {
                transform.rigidbody2D.velocity = (new Vector2 (-speed, speed));
                }
                if (Input.GetKey (KeyCode.S) && Input.GetKey (KeyCode.D)) {
                transform.rigidbody2D.velocity = (new Vector2 (speed, speed));
                }
                if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.D)) {
            transform.rigidbody2D.velocity = (new Vector2 (speed, speed));
                }
                if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.A)) {
                transform.rigidbody2D.velocity = (new Vector2 (-speed, -speed));
                }

    }


}

What happens when I run it is, that I hit the duff, then my player just keeps moving in the direction (unable to use the duffMovement() method) for a few seconds then I'm able to again.

Upvotes: 0

Views: 41

Answers (1)

Atra Viator
Atra Viator

Reputation: 535

You're executing your duff movement after the time period you want to use it has elapsed;

if (hitDuff) {
        timer += Time.deltaTime;
        duffMovement();

        if (timer > 5.0) {
            start ();
            hitDuff = false;
        }
}

Upvotes: 1

Related Questions