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 think 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 this?

O hai ther! I fixed some of your indentation and formatting.

Somehow Statement you manage to crack me up. :)

4 Answers

4

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 this be set to true??
    }

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

Oh yeah, never noticed that!

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.

It already has a collider attached to it!

And I just fixed the script!

I tested it with the fixed script and it still doesn't work. . .I've noticed when you translate an object it will just go through every collider it passes. I think that's the problem, but I don't know how I should make my car move, I tried using a rigidbody, and having it's velocity go torward a waypiont attached to the car, but iTween will make the rigidbody kinematic. . .

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 this, 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}
        );
}

I'm sorry, it still doesn't work. I'm thinking that becuase I'm translating the car it ignores collision (the car goes through buildings), maybe instead of using iTween, just rotate it normally! I'm trying it.

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