I’ve tried to figure this out. I want to make the player not to move inside the helicopter, what should I do? Here’s the code.
using UnityEngine;
using System.Collections;
public class HeliCam : MonoBehaviour {
//Variables start_____________
public Transform myTransform;
//heliTransform = Null GameObject with tag Helicopter
public Transform heliTransform;
//Variables End_______________
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject heli = GameObject.FindGameObjectWithTag("Helicopter");
heliTransform = heli.transform;
}
// Update is called once per frame
void Update () {
Debug.DrawLine(heliTransform.position,myTransform.position);
if(Input.GetKeyDown(KeyCode.E) Vector3.Distance(myTransform.position,heliTransform.position) < 10) {
myTransform.position = heliTransform.position;
myTransform.rotation = heliTransform.rotation;
}
}
}
I assume you are trying to make a system where you can enter a vehicle etc.
The most basic course of action you need to take is to parent your player to the helicopter.
gameObject.transform.parent = heliTransform;
This is only half the battle though, if you parent an object to another, moving the child object individually will move it relative to the parent, without affecting the parent. But if you move the parent gameobject, any and all child gameobjects attached to it will also move.
So to move the helicopter without having the character move “inside” the helicopter, you need to affect the position of the helicopter(parent) instead of the player object(child)
Thank you very much :), but could you still help me just a little bit. When I make this, it only keeps it while I press E. Is there a way to fix this? Bool maby?