I’m trying to get my player (car) to move towards the right junction of a road according to user Input: left, forward or right. I’ve tried several different solution but all have the same effect. Anyone know why?
Junction:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JunctionSys : MonoBehaviour
{
[SerializeField] public Transform NorthJunction;
[SerializeField] public Transform EastJunction;
[SerializeField] public Transform SouthJunction;
[SerializeField] public Transform WestJunction;
[Space]
public GameObject North;
public GameObject East;
public GameObject South;
public GameObject West;
[Space]
public CarSystem player;
public string nextName;
public bool turnable;
[Space]
[SerializeField] public Transform target;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
turnable = true;
}
}
void ChooseDirection()
{
if(target == NorthJunction)
{
nextName = "N";
}
if(target == EastJunction)
{
nextName = "E";
}
if(target == SouthJunction)
{
nextName = "S";
}
if(target == WestJunction)
{
nextName = "W";
}
player.Restart();
}
public void Forward()
{
if(turnable == true)
{
if(player.GoingNorth == true && North != null)
{
target = NorthJunction;
ChooseDirection();
}
if(player.GoingEast == true && East != null)
{
target = EastJunction;
ChooseDirection();
}
if(player.GoingSouth == true && SouthJunction != null)
{
target = SouthJunction;
ChooseDirection();
}
if(player.GoingWest == true && WestJunction != null)
{
target = WestJunction;
ChooseDirection();
}
}
}
public void Right()
{
if(turnable == true)
{
if(player.GoingNorth == true && EastJunction != null)
{
target = EastJunction;
ChooseDirection();
}
if(player.GoingEast == true && SouthJunction != null)
{
target = SouthJunction;
ChooseDirection();
}
if(player.GoingSouth == true && WestJunction != null)
{
target = WestJunction;
ChooseDirection();
}
if(player.GoingWest == true && NorthJunction != null)
{
target = NorthJunction;
ChooseDirection();
}
}
}
public void Left()
{
if(turnable == true)
{
if(player.GoingNorth == true && WestJunction != null)
{
target = WestJunction;
ChooseDirection();
}
if(player.GoingEast == true && NorthJunction != null)
{
target = NorthJunction;
ChooseDirection();
}
if(player.GoingSouth == true && EastJunction != null)
{
target = EastJunction;
ChooseDirection();
}
if(player.GoingWest == true && SouthJunction != null)
{
target = SouthJunction;
ChooseDirection();
}
}
}
}
Car:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CarSystem : MonoBehaviour
{
public bool GoingNorth;
public bool GoingSouth;
public bool GoingEast;
public bool GoingWest;
[Space]
public JunctionSys junction;
public JunctionSys nextJunction;
public JunctionSys startJunction;
[Space]
public Transform Aim;
public Transform Player;
public Transform startJunctionObj;
[Space]
[Space]
public bool drive;
public float speed = 15f;
public float turnSpeed = 1f;
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
if(drive == true)
{
var step = speed * Time.deltaTime;
Player.transform.position = Vector3.MoveTowards(transform.position, Aim.transform.position, step);
/*var step = speed * Time.deltaTime;
var movePos = Vector3.Lerp(transform.position, Aim.transform.position, 0.125f);
Player.transform.position = Vector3.MoveTowards(transform.position, movePos, step);*/
/*Vector3 targetDirection = Aim.position - transform.position;
float singleStep = turnSpeed * Time.deltaTime;
Vector3 newDirection = Vector3.RotateTowards(transform.forward, targetDirection, singleStep, 0.0f);
transform.rotation = Quaternion.LookRotation(newDirection);*/
Vector3 direction = Aim.position - transform.position;
Quaternion rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, turnSpeed * Time.deltaTime);
}
}
public void Startup()
{
Aim = startJunctionObj;
junction = startJunction;
drive = true;
GoingNorth = true;
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("junction"))
{
drive = false;
}
}
public void Restart()
{
if(junction.nextName == "N")
{
Aim = junction.NorthJunction;
nextJunction = junction.North.GetComponent<JunctionSys>();
ChangeGoing("N");
}
if(junction.nextName == "E")
{
Aim = junction.EastJunction;
nextJunction = junction.East.GetComponent<JunctionSys>();
ChangeGoing("E");
}
if(junction.nextName == "S")
{
Aim = junction.SouthJunction;
nextJunction = junction.South.GetComponent<JunctionSys>();
ChangeGoing("S");
}
if(junction.nextName == "W")
{
Aim = junction.WestJunction;
nextJunction = junction.West.GetComponent<JunctionSys>();
ChangeGoing("W");
}
Debug.Log(junction.nextName);
junction = nextJunction;
drive = true;
}
public void Fwrd()
{
junction.Forward();
}
public void Lft()
{
junction.Left();
}
public void Rit()
{
junction.Right();
}
void ChangeGoing(string dir)
{
switch(dir)
{
case "N":
GoingNorth = true;
GoingEast = false;
GoingSouth = false;
GoingWest = false;
break;
case "E":
GoingNorth = false;
GoingEast = true;
GoingSouth = false;
GoingWest = false;
break;
case "S":
GoingNorth = false;
GoingEast = false;
GoingSouth = true;
GoingWest = false;
break;
case "W":
GoingNorth = false;
GoingEast = false;
GoingSouth = false;
GoingWest = true;
break;
}
}
}