I checked with a debug.log and it still runs the line of code for movement, but something is restricting the movement after I collide with a boundary.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private Collider[] Boundary;
void Start ()
{
GameObject[] walls = GameObject.FindGameObjectsWithTag("Boundary");
Boundary = new Collider[walls.Length];
for (int i = 0; i < Boundary.Length; i++)
{
Boundary[i] = walls[i].GetComponent<Collider>();
}
}
void Update ()
{
movement();
}
void movement ()
{
Vector3 CarPosition = new Vector3(0, 0, 0);
//Car controls
Vector3 translation = new Vector3(0, 0);
if (Input.GetKey(KeyCode.W))
{
CarPosition = new Vector3(0, 250) * Time.deltaTime; //Y axis altered by 250 over time
}
if (Input.GetKey(KeyCode.S))
{
CarPosition = new Vector3(0, -250) * Time.deltaTime; //Y axis altered by 250 over time
}
if (Input.GetKey(KeyCode.A))
{
CarPosition = new Vector3(-250, 0) * Time.deltaTime; //X axis altered by 250 over time
}
if (Input.GetKey(KeyCode.D))
{
CarPosition = new Vector3(250, 0) * Time.deltaTime; //X axis altered by 250 over time
}
// loop through each boundary and check if the new translation would intersect the walls
for (int i = 0; i < Boundary.Length; i++)
{
if (new Bounds(GetComponent<Collider>().bounds.center + translation * Time.deltaTime, GetComponent<Collider>().bounds.size).Intersects(Boundary[i].bounds))
{
//if the collision is detected, return the method here
return;
}
}
// else add the translation to the car
transform.Translate(CarPosition);
}
}
Where’s the problem?