Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PauloPatez · Sep 24, 2011 at 07:44 PM · collisionvariableloop

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; } }

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image goosoodude · Sep 24, 2011 at 07:46 PM 0
Share

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

avatar image PauloPatez · Sep 24, 2011 at 08:21 PM 0
Share

Sorry. Is it more understandable now?

avatar image PauloPatez · Sep 25, 2011 at 02:37 AM 0
Share

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);

}

avatar image timberland8989 · Sep 25, 2011 at 03:31 AM 0
Share

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

timberland

timberland boot

timberland shops

timberland 2011

discount timberland

wholesale timberland

timberland chaussures

avatar image PauloPatez · Sep 26, 2011 at 12:08 AM 0
Share

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

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PauloPatez · Sep 25, 2011 at 02:37 AM 0
Share

It works, thanks.

avatar image
0

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
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);
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PauloPatez · Sep 24, 2011 at 08:34 PM 0
Share

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".

avatar image asafsitner · Sep 24, 2011 at 08:37 PM 0
Share

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.

avatar image PauloPatez · Sep 24, 2011 at 10:01 PM 0
Share

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

avatar image msknapp · Sep 25, 2011 at 02:10 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

Accessing variable from another script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges