Problem accessing variable in javascript

Hi everyone, I’m trying to make a sphere (testObject1) change direction when collides with a rigidbody.

The idea is to keep the sphere bouncing from 1 point to another (testObject2 and testObject3). So, I came up with this:


var speed = 0.10;
var dropPoint = 1;

function Update () {
	
	if(dropPoint == 1){
		transform.Translate(0, 0, speed);
	}else{
		transform.Translate(0, 0, speed - (speed * 2));
	}	
}

function OnCollisionEnter(collision : Collision) {
    for (var contact : ContactPoint in collision.contacts) {
    
        if(collision.collider == "testObject3"){
			dropPoint = 2;
        }else{
			if(collision.collider == "testObject2"){
				dropPoint = 1;
			}
        }
        if(collision.collider != "testFloor"){
			print(collision.collider);
        }
    }
}

The problem is that when the Sphere collides with testObject3, it just keeps going in the same direction and not in the opposite one as it should go.

What happening here? Any idea?

Thanks!

The majority of your problems are in this function:

function OnCollisionEnter(collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {

        if(collision.collider == "testObject3"){
         dropPoint = 2;
        }else{
         if(collision.collider == "testObject2"){
          dropPoint = 1;
         }
        }
        if(collision.collider != "testFloor"){
         print(collision.collider);
        }
    }

I’m going to go line by line and explain what’s going on here:

  • function ...{
    • This line is done perfectly. Moving on…
  • foreach( ContactPoint ) {
    • Huh? Why are you trying to loop through the points? You never use any of them! This loop does nothing. You only need to evaluate the body once
  • if(collider == "testObject3")
    • This will never evaluate to true. Why? Because you’re comparing a collider with a string. What you’re trying to do is
    • if(collider.**name** == "testObject3")
    • Now you’re comparing a string with a string. This can and will evaluate to true.
  • There are several repeats of the above problem
  • if(collision.collider != "testFloor")
    • Again, this will never evaluate to false. If you always want this to print, then just remove the if in front of print altogether.

The only other thing I can add is that I would change your Update function to this, it looks more professional, and is easier to edit:

PLEASE DO NOT COPY-PASTE THIS! RETYPE IT BY HAND. It seems tedious, but trust me, it helps. You don’t need to retype the comments, they exist so that you understand what you are typing.

//this function is called once per frame.
function Update ()
{
    //switch-case is shorthand for if(dropPoint==1){}else if(==2){}else if(=="etc"){}else
    switch(dropPoint)
    {
        //if you forget the break, it will do case 1, then do case two, then do case 3...
        case 1:
            transform.Translate(0, 0,  speed * Time.deltaTime); break; 
        case 2:
            transform.Translate(0, 0, -speed * Time.deltaTime); break;
        default: /*if none of the above cases are true, do nothing*/ break;
    }
    //you can have more than one action per case, as long as there is a break; at the end
}