So I’m making a kinda medieval game in Unity. Just finished editing the Car Script a lot of people us and I realize I can enter the car/horse from wherever. I’ve tried to edit the script so only when i’m in the collision zone it will allow me to enter, but it still won’t! Heres the script (Works, but can enter anywhere)
#pragma strict
var Car : Transform;
var player : Transform;
var exitPoint : Transform;
var doorTriggerLeft : Transform;
var PlayerCamera : Camera;
var CarCamera : Camera;
var isPlayerVisable : boolean;
var fly : Transform;
function Start (){
fly.GetComponent("fly").active = false;
}
function Update (){
//enter is e
if (Input.GetButtonDown("enter")&& isPlayerVisable)
{
//Make player invisable and still standing
player.gameObject.SetActiveRecursively(false);
player.gameObject.active = false;
// Parent player to ExitPoint
player.parent = exitPoint.transform;
player.transform.localPosition = Vector3(3,2,0);
//Parent playerParent to car
exitPoint.parent = Car.transform;
exitPoint.transform.localPosition = Vector3(0,0,0);
// Enable car as controllable object
fly.GetComponent("fly").active = true;
PlayerCamera.active = false;
CarCamera.active = true;
}
else
{
//exit is r
if (Input.GetButtonDown("Exit")){
// Make Character visable again.
player.gameObject.SetActiveRecursively(true);
player.gameObject.active = true;
// Unparent Player from everything.
player.transform.parent = null;
// Parent Exit Point to Door Trigger.
exitPoint.parent = doorTriggerLeft.transform;
// Disable car as a controllable
fly.GetComponent("fly").active = false;
PlayerCamera.active = true;
CarCamera.active = false;
}
}
}
function OnTriggerEnter(Player : Collider) {
isPlayerVisable = true;
}
function OnTriggerExit(Player : Collider) {
isPlayerVisable = false;
}
P.S. Fly is the script that I use to move the horse. Here is the code to that:
#pragma strict
var rotateSpeed = 25.0;
var speed = 50.0;
var Horse : Transform;
function Update() {
var transAmount = speed * Time.deltaTime;
var rotateAmount = rotateSpeed * Time.deltaTime;
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (Input.GetKey("up")) {
Horse.rigidbody.AddForce (transform.forward * 30);
}
if (Input.GetKey("down")) {
Horse.rigidbody.AddForce (transform.forward * -30);
}
if (Input.GetKey("left")) {
Horse.transform.Rotate(0, -rotateAmount, 0);
}
if (Input.GetKey("right")) {
Horse.transform.Rotate(0, rotateAmount, 0);
}
}