Good afternoon Unity community. It’s me again, got an issue here with a dinosaur I’ve made out of bricks. The git is refusing to do what I’m telling to and I’ve found that the distance calculations in my script aren’t working for some reason. Here’s my script;
#pragma strict
// this is timmy's sat-nav.
var speed : float = 4.0; // timmy should have a default speed of 2 meters/second
var rotatespeed = 0.5; // the speed that timmy should rotate at
var timmy : GameObject; // timmy will be found and assigned to this variable
timmy = GameObject.Find("Timmy");
// this didn't work, time for something new
// var Waypoints = new Array[6]; // this is the waypoint timmy is currently targetting
// var Index = 0; // this is the index for the Waypoints array
var target = 0; // Timmy's current target waypoint
var distance : float; // this variable records how far timmy is from his target waypoint
var changingTarget : boolean;
// preparing the waypoint variables -
// these need to be prepared in the editor before they can be used in the game
var Waypoint0 : GameObject;
var Waypoint1 : GameObject;
var Waypoint2 : GameObject;
var Waypoint3 : GameObject;
var Waypoint4 : GameObject;
var Waypoint5 : GameObject;
// set the elements of the array to equal the variables we have provided
/*
Waypoints[0] = Waypoint0;
Waypoints[1] = Waypoint1;
Waypoints[2] = Waypoint2;
Waypoints[3] = Waypoint3;
Waypoints[4] = Waypoint4;
Waypoints[5] = Waypoint5;
*/
function Start ()
{
speed = 2; // reset timmy to his default speed if it's a new instance of him
changingTarget = false;
// assign waypoints to the elements of the Waypoints array again -
// this needs to be done in the Start function because it runs every time the script
// is run. We could do this outside all functions, but the script would only work the
// first time it was used - aka the prehistoric level. As soon as we loaded a new level
// that had this script the objects referenced would not exist any more.
/*
Waypoints[0] = Waypoint0;
Waypoints[1] = Waypoint1;
Waypoints[2] = Waypoint2;
Waypoints[3] = Waypoint3;
Waypoints[4] = Waypoint4;
Waypoints[5] = Waypoint5;
*/
}
function Update ()
{
// rotate to aim at target depending on which targets timmy has met
LookAtTarget(target);
// calculate how far away Timmy is from his current target
CalculateDistance(target);
Debug.Log(distance);
// check if Timmy needs to change target and act appropriately if he does
NextTarget();
// and move timmy forward at the speed we set for him...
transform.Translate((speed * Time.deltaTime), 0, 0);
}
function NextTarget()
{
if (distance < 2) // if timmy is getting close to his target
{
if (changingTarget == false) // and if timmy is not already changing target...
{
changingTarget = true; // remember that timmy is changing target
Debug.Log("Getting a bit close, eh Timmy?"); // let us know he's getting close
if (target == 5) // if we're at the end of the array...
{
// Index = 0; // go back to the beginning
target = 0;
}
else // if we're not at the end of the array
{
// Index++; // the index for the array moves along, giving timmy a new target
target++;
}
changingTarget = false; // timmy is no longer changing target, go back to previous state
}
}
}
// this function calculates how far away Timmy is from his current target
function CalculateDistance(currentTarget)
{
switch (currentTarget)
{
case 0:
// calculate the distance between timmy and his current target
var WaypointPoints = (Waypoint0.transform.position.x - Waypoint0.transform.position.z);
var TimmyPoints = (timmy.transform.position.x - timmy.transform.position.z);
var distance = (TimmyPoints - TimmyPoints);
break;
case 1:
// calculate the distance between timmy and his current target
//distance = Vector3.Distance(Waypoint1.transform.position, timmy.transform.position);
break;
case 2:
// calculate the distance between timmy and his current target
//distance = Vector3.Distance(Waypoint2.transform.position, timmy.transform.position);
break;
case 3:
// calculate the distance between timmy and his current target
//distance = Vector3.Distance(Waypoint3.transform.position, timmy.transform.position);
break;
case 4:
// calculate the distance between timmy and his current target
//distance = Vector3.Distance(Waypoint4.transform.position, timmy.transform.position);
break;
case 5:
// calculate the distance between timmy and his current target
//distance = Vector3.Distance(Waypoint5.transform.position, timmy.transform.position);
break;
}
}
function LookAtTarget(whichTarget)
{
switch (whichTarget)
{
case 0:
// rotate to look at the target provided from current rotation
transform.rotation = Quaternion.Slerp (transform.rotation, Waypoint0.transform.rotation, Time.time * rotatespeed);
break;
case 1:
// rotate to look at the target provided from current rotation
transform.rotation = Quaternion.Slerp (transform.rotation, Waypoint1.transform.rotation, Time.time * rotatespeed);
break;
case 2:
// rotate to look at the target provided from current rotation
transform.rotation = Quaternion.Slerp (transform.rotation, Waypoint2.transform.rotation, Time.time * rotatespeed);
break;
case 3:
// rotate to look at the target provided from current rotation
transform.rotation = Quaternion.Slerp (transform.rotation, Waypoint3.transform.rotation, Time.time * rotatespeed);
break;
case 4:
// rotate to look at the target provided from current rotation
transform.rotation = Quaternion.Slerp (transform.rotation, Waypoint4.transform.rotation, Time.time * rotatespeed);
break;
case 5:
// rotate to look at the target provided from current rotation
transform.rotation = Quaternion.Slerp (transform.rotation, Waypoint5.transform.rotation, Time.time * rotatespeed);
break;
}
}
Originally I tried to use an array but I had some issues with the code and decided that since I wouldn’t need to add any more waypoints the flexibility wasn’t necessary - as such I’ve tried simplifying the code and I’ve encountered another problem.
As you can see I’ve got a function running that should be updating the distance variable every frame (the function is being run from the Update function) but for some reason in the editor the distance is remaining 0 and the dinosaur’s targets are cycling ridiculously fast, causing him to move sideways. I’ve also discovered that when I run the game and try to rotate the dinosaur in the scene window as he is moving it doesn’t work - I suspect this is because of the quaternion slerp moving him constantly though.
Can anybody work out what’s wrong with my code? As far as I can see he should be working out how far away his target is then aiming at the next one if he gets closer than 3 units, but for some reason it’s not happening.
Thanks in advance.
Regards, FreakFish ~