- Home /
Vehicle Script Modifications
How do I modify this script so that a projectile is emitted from a gameObject when the player presses enter, but only when the player is in the vehicle?
using UnityEngine;
using System.Collections;
public class Vehicle : MonoBehaviour {
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;
}
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");
}
}
}
}
I guess you would just put the the part to create the projectile inside of the statement on line 37 (if (inVehicle) {}) I guess you would put inside there, if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Enter)) {and you write the code to send the projectile here} also, I don't know what the $$anonymous$$eyCode for Enter is, search it for yourself you will probably find it
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
I need help instantiateing a bullet 0 Answers
Problem with vehicle script 1 Answer
Saving a value if it is larger than the current value... 3 Answers
Vehicle Script Debugging 1 Answer