- Home /
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; } }
Could you format your script properly? Then we will be able to read it.
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.$$anonymous$$ove(moveDirection * Time.deltaTime);
}
Thanks for co$$anonymous$$g 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
I fixed it, the problem was in the moveDirection values, only "moveDirection = Vector3(speed, 0, 0);" is enough.
Answer by leonardo_try · Sep 24, 2011 at 08:31 PM
well, if your problem is in the comparison, try to do this:
if(moveLeft){
moveLeft = false;
moveRight = true;
} else {
moveLeft = true;
moveRight = false;
}
Answer by msknapp · Sep 24, 2011 at 08:34 PM
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);
}
Answer by asafsitner · Sep 24, 2011 at 08:23 PM
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);
}
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".
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; ins$$anonymous$$d. Edited the answer to reflect that.
Do not works yet. I did it with the leonardo's solution.
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.
Your answer
Follow this Question
Related Questions
Collision object and access to a script variable? 1 Answer
GUI.Box within a Collider 1 Answer
Call a Void on Collision 2 Answers
Global Varible Problem 2 Answers