x


What is the problem in my script?

First of all, I'll explain what I want to do. It's a zombie AI script for a puzzle game, I want that when the zombie collide with a specified (tagged) object, the direction witch he's walking be reversed, in other words, if he's walking to the left, when collide with an obstacle, will start to walk to the right until it reaches another obstacle, then will move to left again, it's like a infinite loop. Here's my script:

private var speed : float = 1;   //Walk velocity
private var moveLeft = true;     //"The character is moving to left"
private var moveRight = false;   //"The character is moving to right"
var toLeftSpeed : float = -1;    //Walk velocity to left
var toRightSpeed : float = 1;    //Walk velocity to right

function OnCollisionEnter(Zombie : Collision) {
	if(Zombie.tag == "obst") {    //If it collide with the obstacle
		if (moveLeft == true) {  //If it's walking to left
			moveRight == true;  //It will start to walk to right
		}
		if (moveRight == true) {	 //If it's walking to right
			moveLeft == true;	 //It will start to walk to left
		}
	}
}

function Update() {
	transform.translate(speed * Time.deltaTime, 0, 0);
	if (moveLeft == true) {
		speed = toLeftSpeed;
	}
	if (moveRight == true) {
		speed = toRightSpeed;
	}
}
more ▼

asked Sep 24, 2011 at 07:44 PM

PauloPatez gravatar image

PauloPatez
3 16 13 13

Could you format your script properly? Then we will be able to read it.

Sep 24, 2011 at 07:46 PM goosoodude

Sorry. Is it more understandable now?

Sep 24, 2011 at 08:21 PM PauloPatez

Now I have other problem, both for positive and for negative values of var speed, the object moves to the same direction. Here's the modified script:

var speed = -1;

var toLeftSpeed = -1;

var toRightSpeed = 1;

function OnTriggerEnter(collision : Collider) {

if (collision.gameObject.tag == "obst") {

    Debug.Log ("collide");

    if (speed == toLeftSpeed) {

        speed = toRightSpeed;

    } else {

        speed = toLeftSpeed;

    }



}

}

function Update() {

moveDirection = Vector3(speed, 0, 0);

moveDirection = transform.TransformDirection(moveDirection);

moveDirection *= speed;



var controller : CharacterController = GetComponent(CharacterController);

controller.Move(moveDirection * Time.deltaTime);

}

Sep 25, 2011 at 02:37 AM PauloPatez

Thanks for coming our timberland online store! Just enjoy yourself here! As a developping company, timberland boots always can give us some surprise .I belive timberland shoes Sale can make your feet more comfortable, make your life more stylish!If you want to have a try ,just click here: discount timberland boots Free delivery

timberland timberland boot timberland shops timberland 2011 discount timberland wholesale timberland timberland chaussures
Sep 25, 2011 at 03:31 AM timberland8989

I fixed it, the problem was in the moveDirection values, only "moveDirection = Vector3(speed, 0, 0);" is enough.

Sep 26, 2011 at 12:08 AM PauloPatez
show all comments (comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

well, if your problem is in the comparison, try to do this:

if(moveLeft){

 moveLeft = false;
 moveRight = true;

} else {

 moveLeft = true;
 moveRight = false;

}

more ▼

answered Sep 24, 2011 at 08:31 PM

leonardo_try gravatar image

leonardo_try
61 26 22 22

It works, thanks.

Sep 25, 2011 at 02:37 AM PauloPatez
(comments are locked)
10|3000 characters needed characters left

At first glance, try this:

function OnCollisionEnter(Zombie : Collision) {

if(Zombie.tag == "obst") {    //If it collide with the obstacle

    if (moveLeft == true) {  //If it's walking to left

       moveRight == true;  //It will start to walk to right

       moveLeft=false;
       speed = toRightSpeed;
    }

    if (moveRight == true) {    //If it's walking to right

       moveRight == false;    //It will start to walk to right

       moveLeft=true;

         speed = toLeftSpeed;
    }

}

}

function Update() {
     transform.translate(speed * Time.deltaTime, 0, 0);
}
more ▼

answered Sep 24, 2011 at 08:34 PM

msknapp gravatar image

msknapp
286 23 20 21

(comments are locked)
10|3000 characters needed characters left

I took the liberty to copy your code and format it a bit. It appears the problem is with your flow - in the OnCollisionEnter function you first check to see if moveLeft is true, and if so set moveRight to true. However, you then independently check right afterwards if moveRight is true, and it is! so you set it to false and negate the first if check. What you need to do is make it an else-if check instead. You may also want to move the Translate function to the end, until after you've determined the walk direction.

private var speed : float = 1;
private var moveLeft = true;
private var moveRight = false;
var toLeftSpeed : float = -1;
var toRightSpeed : float = 1;

function OnCollisionEnter(Zombie : Collision) 
{
    if(Zombie.tag == "obst") 
    {
        if (moveLeft == true) 
        {      //the problem is here
            moveRight = true;  //here
            moveLeft = false;
        }

        else if (moveRight == true) 
        {       //here
               moveRight = false;    //and here
            moveLeft = true;
        }

    }   
}

function Update() 
{
    if (moveLeft == true) 
    {
        speed = toLeftSpeed;
    }
    if (moveRight == true) 
    {
        speed = toRightSpeed;
    }
    transform.translate(speed * Time.deltaTime, 0, 0);
}
more ▼

answered Sep 24, 2011 at 08:23 PM

asafsitner gravatar image

asafsitner
3.3k 13 17 35

Sorry, that was just a typo, I already edited it, but that is not the problem with the script, the problem is in the same lines, the log error says "Expressions in statements must only be executed for their side-effects".

Sep 24, 2011 at 08:34 PM PauloPatez

I missed that too. You have another typo: moveRight == false; after the if statement. That's the comparison operator, not the assignment one. Should be moveRight = false; instead. Edited the answer to reflect that.

Sep 24, 2011 at 08:37 PM asafsitner

Do not works yet. I did it with the leonardo's solution.

Sep 24, 2011 at 10:01 PM PauloPatez

I don't think you want to change the speed from the update function, it does not change every frame. Change the speed the same time you change the direction, just copy that code to change the speed to the end of the OnCollisionEnter function.

Sep 25, 2011 at 02:10 PM msknapp
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4679
x4017
x1314
x1273
x506

asked: Sep 24, 2011 at 07:44 PM

Seen: 1033 times

Last Updated: Sep 26, 2011 at 01:47 PM