- Home /
Vehicle Script Debugging
I have the following script, that SHOULD allow me to get into and out of a vehicle, but I can't get out once I get in. Help?
using UnityEngine;
using System.Collections;
public class Vehicle : MonoBehaviour {
//public float bulletSpeed = 10.0F;
//public GameObject projectile;
//public float getInRange = 6.0F;
public Camera VehicleCamera;
public GameObject Player;
private bool InVehicle;
private bool VehicleControl;
public float Speed = 80.0F;
public float RotationSpeed = 100.0F;
public Transform _Player;
private float Distance;
public Transform _Vehicle;
private Vector3 VehiclePos;
void Start()
{
VehicleCamera.gameObject.SetActive(false);
}
void Update()
{
Distance = Vector3.Distance(_Player.position, transform.position);
if(Distance < 6)
{
if(Input.GetKeyDown(KeyCode.Tab))
{
InVehicle = true;
}
}
else if(InVehicle)
{
if(Input.GetKeyDown(KeyCode.Tab))
{
InVehicle = false;
VehiclePos = new Vector3(_Vehicle.transform.position.x + 5, _Vehicle.transform.position.y, _Vehicle.transform.position.z);
_Player.transform.position = VehiclePos;
}
}
if(InVehicle)
{
VehicleCamera.gameObject.SetActive(true);
Player.gameObject.SetActive(false);
VehicleControl = true;
//if (Input.GetKeyDown(KeyCode.Return)) {
//var clone = Rigidbody;
//clone = Instantiate(projectile, transform.position, transform.rotation);
//clone.velocity = transform.TransformDirection (Vector3.forward * bulletSpeed);
//}
}
else
{
VehicleCamera.gameObject.SetActive(false);
Player.gameObject.SetActive(true);
VehicleControl = false;
}
if(VehicleControl)
{
float tr = Input.GetAxis("Vertical") * Speed;
float rot = Input.GetAxis("Horizontal") * RotationSpeed;
tr *= Time.deltaTime;
rot *= Time.deltaTime;
transform.Translate(0, 0, tr);
transform.Rotate(0, rot, 0);
}
}
void OnGUI()
{ if(InVehicle == false)
{
if(Distance < 6)
{
GUILayout.Label("Press TAB");
}
}
}
}
Answer by roojerry · Aug 21, 2013 at 05:37 PM
if(Distance < 6)
{
if(Input.GetKeyDown(KeyCode.Tab))
{
InVehicle = true;
}
}
else if(InVehicle)
{
quick issue I see here is that you can never check for input to get out of the car because while inside the car, you will always be at a distance less than 6 from the car
Why is everyone on the answers and forum so bloody unhelpful??? This is my 5th repost!!!
Unhelpful? What effort did you put in to debug this script? I took a quick glance at the wall of code you posted and noticed a problem and pointed you towards that problem. If you couldnt take the time to really look into your issue, then why should I take my time to rewrite your broken code for you.
Atleast give some thought to my answer and try to learn why you are having this issue, so you will know what to do next time. Otherwise, wait for some more helpful person to give you the quick and lazy answer you want.
Fine I'm sorry. I'm just kind of under a deadline crunch. I need to get this done.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Vehicle Script Modifications 0 Answers
Vehicle Script Help 1 Answer
Problem with vehicle script 1 Answer
Vehicle/car/ Movement 0 Answers