Mark Zareal
Mark Zareal

Reputation: 23

Character moving code not working- Unity3D unityscript

I have a script that moves the character in directions depends on what it collides with. Heres my code:

var speed : float = 1;
private var leftBool : boolean;
private var rightBool : boolean;
private var upBool : boolean;
private var downBool : boolean;

function Update () {
    if(leftBool == true) {
        gameObject.GetComponent(Rigidbody2D).velocity.x =         gameObject.GetComponent(Rigidbody2D).velocity.x - speed * Time.deltaTime;
        rightBool = false;
        upBool = false;
        downBool = false;
    }
    if(upBool == true) {
        gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y + speed * Time.deltaTime;
        rightBool = false;
        leftBool = false;
        downBool = false;
    }
    if(rightBool == true) {
        gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x + speed * Time.deltaTime;
        rightBool = false;
        leftBool = false;
        downBool = false;
    }
    if(downBool == true) {
        gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y - speed * Time.deltaTime;
        rightBool = false;
        leftBool = false;
        upBool = false;
    }   
}

function Start () {
    leftBool = true;
}


function OnTriggerEnter2D (other : Collider2D) {
    if(other.GetComponent(Arrows).rotationNumber == 1) {
        leftBool = true;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 2) {
        upBool = true;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 3) {
        rightBool = true;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 4) {
        downBool = true;
        Debug.Log("Entered");
    }
}

It shows "Entered" in the debug.log but the character doesn't change directions when it collides. Why does this happen? Thanks.

Upvotes: 0

Views: 759

Answers (1)

Imtiaj Ahmed
Imtiaj Ahmed

Reputation: 350

At first when colliding suppose with rotationNumber==1, leftBool remains true. Others are false as per your code in declaration and Update(). Now, when you collide with rotationNumber==2, upBool returns true. But as earlier, leftBool is still true in Update(), and it is on the top of the code execution order, upBool will return to false immediately. So, you will never get if(upBool == true){} things (By the way, you don't have to use == operator for bools, if(upBool) for true and if(!upBool) for false). Hope it will clear up your confusion. Now, there are lot of ways to achieve what you want. But, here is something I modified your codes so that you may understand what's going on -

var speed : float = 1;
private var leftBool : boolean;
private var rightBool : boolean;
private var upBool : boolean;
private var downBool : boolean;

function Start () {
    leftBool = true;
}

function Update () {
    if(leftBool) {
        gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x - speed * Time.deltaTime; 
    }
    if(upBool) {
        gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y + speed * Time.deltaTime;   
    }
    if(rightBool) {
        gameObject.GetComponent(Rigidbody2D).velocity.x = gameObject.GetComponent(Rigidbody2D).velocity.x + speed * Time.deltaTime;
    }
    if(downBool) {
        gameObject.GetComponent(Rigidbody2D).velocity.y = gameObject.GetComponent(Rigidbody2D).velocity.y - speed * Time.deltaTime;
    }   
}

function OnTriggerEnter2D (other : Collider2D) {
    if(other.GetComponent(Arrows).rotationNumber == 1) {
        leftBool = true;
        rightBool = false;
        upBool = false;
        downBool = false;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 2) {
        upBool = true;
        rightBool = false;
        leftBool = false;
        downBool = false;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 3) {
        rightBool = true;
        upBool = false;
        leftBool = false;
        downBool = false;
        Debug.Log("Entered");
    }
    if(other.GetComponent(Arrows).rotationNumber == 4) {
        downBool = true;
        rightBool = false;
        leftBool = false;
        upBool = false;
        Debug.Log("Entered");
    }
}

Hope it will help.

Upvotes: 1

Related Questions