help in script...

hi iam doing mario’s gumba movement in which the gumba has to move in a direction and when it collides with any static objects it has to reverse its movement.

the problem in this script is that the gumba’s movement works for only one cycle, (i.e) it moves in left direction and when it hits a static object it reverses its movement to right and when it hits something at right it never reverses back, it just stays there. I couldn’t able to figure out wat the problem is and the logic is.need help

the script is as follows,

using UnityEngine;
using System.Collections;

public class bug : MonoBehaviour {

bool wallHit = false;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	
	if(wallHit)
	{		
	    
		rigidbody.velocity = new Vector3(5,0,0);	
	}
	
	else
	{
		wallHit = false;
		rigidbody.velocity = new Vector3(-5,0,0);
	}
}

 void OnCollisionEnter(Collision other)
{
	if(other.gameObject.name == "static_object")
	{
		wallHit = true;
	}
}

}

Change wallHit=true to wallHit = !wallHit

Your mistake is that whenever it hits a wall object movement is set to go right, instead of changing the walk direction. Also wallHit = false in else is unnecessary.

It would maybe be best if you would just do:

Vector3 moveDirection;
void Start{
    moveDirection = new Vector3(5,0,0);
}
void Update(){
    rigidbody.velocity = moveDirection;
}
void OnCollisionEnter(Collision other){
    if(other.gameObject.name =="static_object")
    {
        moveDirection.x = moveDirection.x * (-1);
    }
}

A few things…

if(wallHit)
    {        
       rigidbody.velocity = new Vector3(5,0,0); 
    }
 
    else
    {
       wallHit = false; //This line of code is redundant as <wallHit> is already false to make it into this case
       rigidbody.velocity = new Vector3(-5,0,0);
    }

Walk this out for yourself step by step.

1)wallHit is initiated to false so the else clause will be active in your Update() function giving your goomba a velocity of -5 (moving left).

2)Now your goomba hits a “static_object”, wallHit is set to true and the other case of your if/else statement takes over causing the goomba to move with a velocity of 5 (right).

3)Now your goomba hits another “static_object”, flagging the already true wallHit to true once more, making your if/else statement still apply velocity to the right.

At no point in your code can wallHit ever be set back to false. And this would not be the most efficient way to achieve the results you desire anyhow as you would need to flag objects as “left_static_object” to make them bounce back right and “right_static_object” to have them bounce back left. It will just end up being a mess.

A better way to go about it would be to create a variable “xVelocity” and when a “static_object” is hit, multiply xVelocity by -1.

int xVelocity = -5;

void Update () {     
    rigidbody.velocity = new Vector3(xVelocity,0,0); //constantly move goomba according to the value of xVelocity        
}
 
 void OnCollisionEnter(Collision other)
{
    if(other.gameObject.name == "static_object")
    {
       xVelocity *= -1; //reverse the direction of the goomba
    }
}