Ok, so I made a script for grid like movement, and it worked before I started working on adding the obstruction via raycasting. As soon as I added raycasting for obstruction detection, I wasn’t getting any errors, but my player is not able to move anymore. I have tried to fix it based on examples, and I have tried this for about a few hours with no luck. Anyone proficient enough in C# to help a new coder like me find the problem? (My code might be a bit of a mess, i just started coding about 2 weeks ago.)
using UnityEngine;
using System.Collections;
public class MoveScript : MonoBehaviour {
public bool forwardObstruction = false; // Next few bools are to register if there is an obstruction-
public bool backwardObstruction = false; // -in the given direction.
public bool leftObstruction = true;
public bool rightObstruction = true;
public float inputDelay = 0.05f; //Stating how many seconds before we can move again.
public Vector3 forwardDirection; //These next few vectors are direction values.
public Vector3 backwardDirection;
public Vector3 leftDirection;
public Vector3 rightDirection;
public int smoothFactor = 2; // Not used yet
public int lineLength = 2; // Not used yet
private float timestamp;
// Update is called once per frame
void Update () {
// Creates a raycast line forward based on lineLength, checks to see if path is obstructed.
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, lineLength)){
forwardObstruction = true;
}
else
forwardObstruction = false;
Vector3 bwd = transform.TransformDirection(Vector3.back);
if (Physics.Raycast (transform.position, bwd, lineLength)) {
backwardObstruction = true;
}
else
backwardObstruction = false;
Vector3 left = transform.TransformDirection(Vector3.left);
if (Physics.Raycast (transform.position, left, lineLength)) {
leftObstruction = true;
}
else
leftObstruction = false;
Vector3 right = transform.TransformDirection(Vector3.right);
if (Physics.Raycast (transform.position, right, lineLength)) {
rightObstruction = true;
}
else
rightObstruction = false;
//See if the "Forward" key is pressed
if (forwardObstruction == true && Time.time >= timestamp && Input.GetButton ("Forward")) {
transform.Translate (forwardDirection);
timestamp = Time.time + inputDelay;
forwardObstruction = forwardObstruction;
}
//See if the "Backward" key is pressed
if (backwardObstruction == true && Time.time >= timestamp && Input.GetButton("Backward")) {
transform.Translate (backwardDirection);
timestamp = Time.time + inputDelay;
}
//See if the "Left" key is pressed
if (Time.time >= timestamp && Input.GetButton ("Left")) {
transform.Translate (leftDirection);
timestamp = Time.time + inputDelay;
}
//See if the "Right" key is pressed
if (Time.time >= timestamp && Input.GetButton ("Right")) {
transform.Translate (rightDirection);
timestamp = Time.time + inputDelay;
}
}
}
Any help is appreciated, thank you for at least looking!