• 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 Rydrako · Mar 27, 2011 at 04:42 PM · triggercar

Need help with city car script!

Hello, I'm trying to make a simple car, in a city, to randomly choose to go left or right at each intersection it passes, I'm using triggers at the intersections. Here's the script for the car -

//Triggers will access the turnNow variable.

var turnNow = false; var speed = 5.0; var turnSpeed = 2.0; private var chance; private var goLeft = false; private var goRight = false;

function Update () { //Drive! transform.Translate(0,0,speed * Time.deltaTime);
chance = Random.Range(0,100);

 if(turnNow)
 {
     if(chance>50)
     {
         //go left!
         goLeft = true;
     }
     else 
     {
         //go right!
         goRight = false;
     }
 }           
 if(goLeft)
 {
     //go left!
     iTween.RotateTo(gameObject,{"y":transform.rotation.y-90,"time":turnSpeed});     
 }
 if(goRight)
 {
     //go right!
     iTween.RotateTo(gameObject,{"y":transform.rotation.y+90,"time":turnSpeed});     
 }           

}

and the trigger script, even though I t$$anonymous$$nk the car script is the problem -

function OnTriggerEnter (collider : Collider) { collider.GetComponent("CarMapper").turnNow = true; }

function OnTriggerExit (collider : Collider) { collider.GetComponent("CarMapper").turnNow = false; }

and the problem is that when the car passes the trigger, turnNow isn't changed. How can I fix t$$anonymous$$s?

Comment
Add comment · Show 2
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 Statement · Mar 27, 2011 at 05:01 PM 2
Share
avatar image FLASHDENMARK · Mar 27, 2011 at 06:11 PM 0
Share

4 Replies

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

Answer by Rydrako · Mar 27, 2011 at 07:02 PM

Thanks you guys! I decided to simplify some t$$anonymous$$ngs and my city cars are great! I just made them immediately turn, instead of trying to script a curve.

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
1

Answer by Meltdown · Mar 27, 2011 at 04:55 PM

You have a small bug in your code, although not sure it will fix your problem...

    if(chance>50)
    {
            //go left!
            goLeft = true;
    }
    else 
    {
            //go right!
            goRight = false;  // shouldn't t$$anonymous$$s be set to true??
    }

Alternatively, have you looked at using SendMessage() to call a method on your CarMapper to set your turnNow variable?

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 Rydrako · Mar 27, 2011 at 05:20 PM 0
Share
avatar image
0

Answer by Statement · Mar 27, 2011 at 05:06 PM

Make sure your car has a collider on it. Also it seems your code would make the car go left-right-left-right-left-right during the turn given you'd fix the bug Meltdown wrote about.

Comment
Add comment · Show 3 · 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 Rydrako · Mar 27, 2011 at 05:11 PM 0
Share
avatar image Rydrako · Mar 27, 2011 at 05:11 PM 0
Share
avatar image Rydrako · Mar 27, 2011 at 05:18 PM 0
Share
avatar image
0

Answer by Eric5h5 · Mar 27, 2011 at 05:12 PM

Your Update function should be:

function Update () {
    transform.Translate(0,0,speed * Time.deltaTime);
}

and that's it, plus you only need the "speed" and "turnspeed" variables. There's no need for the "turnNow" variable, since you can just call a function when needed, instead of checking booleans every frame.

You don't need OnTriggerExit either; you just need to call a function once when the trigger is entered. When doing GetComponent, it's better in most cases not to use quotes (for speed and type safety reasons), also using generics is good practice (easy way to avoid dynamic typing, will work on iOS and Android).

function OnTriggerEnter (collider : Collider) {
        collider.GetComponent.<CarMapper>().TurnNow();
}

Then the TurnNow function in CarMapper could be t$$anonymous$$s, using a ternary operator to get rid of all the if/else code:

function TurnNow () {
    iTween.RotateTo(
        gameObject,
        {"y":transform.rotation.y - 90 * (Random.value < .5? 1 : -1),
        "time":turnSpeed}
        );
}
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 Rydrako · Mar 27, 2011 at 05:50 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Car acceleration using Xbox 360 triggers? 1 Answer

Show laptime after each lap ? 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Car won't return to respawn point after hitting a trigger 2 Answers

Can not get value out of a collision 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