MoveTowards doesn't work correctly
In my game you control a car, while avoiding cars by switching lane. Sometimes in the game a nightclub will come and you have to hold in, to go to the nightclub. I have a boolean called goToNightclub and it becomes true by driving into a trigger object. When the goToNightClub is true, the car moves to a parkingspot using MoveTowards. ' But when i hit the trigger, my car only moves a little bit and not all the way to the spot. Can anyone see something i did wrong? The script is probably a little confusing, and just ignore the comments i made.
#pragma strict
var lane1 : Transform;
var lane2 : Transform;
var lane3 : Transform;
var lane4 : Transform;
var lane5 : Transform;
var nightclub : Transform;
var laneNr = 3;
var speed = 10;
var goToNightclub : boolean = false;
function Start () {
//nightclub = GameObject.FindWithTag("nightClub");
}
function Update () {
var step = speed * Time.deltaTime;
if(goToNightclub != true) {
if(Input.GetKeyDown("a")) {
laneNr = laneNr-1;
}
if(Input.GetKeyDown("s")) {
laneNr = laneNr+1;
}
}
if(laneNr >= 5) {
laneNr = 5;
}
if(laneNr <= 1) {
laneNr = 1;
}
if(laneNr == 1) {
transform.position = Vector3.MoveTowards(transform.position, lane1.position, step);
}
if(laneNr == 2) {
transform.position = Vector3.MoveTowards(transform.position, lane2.position, step);
}
if(laneNr == 3) {
transform.position = Vector3.MoveTowards(transform.position, lane3.position, step);
}
if(laneNr == 4) {
transform.position = Vector3.MoveTowards(transform.position, lane4.position, step);
}
if(laneNr == 5) {
transform.position = Vector3.MoveTowards(transform.position, lane5.position, step);
}
if(goToNightclub == true) {
transform.position = Vector3.MoveTowards(transform.position, nightclub.position, step);
print("THOU");
}
}
function OnTriggerEnter2D (other : Collider2D) {
if(other.gameObject.tag == "EnemyCar") {
Destroy(gameObject);
}
if(other.gameObject.tag == "nightClub") {
print("HEY");
goToNightclub = true;
//speed = 0;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612060103im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
On Trigger Exit Help 1 Answer
If statement not working 1 Answer
how do i get a boolean to switch to true only after i've pressed a key three times? 2 Answers
How do I check if bool is true from another script? 2 Answers
checking a bool via string 0 Answers