- Home /
Problem is not reproducible or outdated
Why is my transform.parent not working
Hello, I want to change my camera's parent to player after the player has exited the ship by pressing "e", but its not working. Here is the code. P.S. I got the player to move after exiting the ship and I got the camera to change parent to ship when player is entering the ship.
if (Input.GetKeyDown("e") && IsInShip && KeyDown == false)
{
camera.parent = player.transform;
camera.localRotation = Quaternion.Euler(0f, 0f, 0f);
camera.localPosition = new Vector3(0f, 0f, 0f);
player.position = ship.position + ship.transform.right * 10f;
player.rotation = Quaternion.Euler(Vector3.up * 90f);
IsInShip = false;
}
if (Input.GetKeyDown("e") && SeesShip && KeyDown == false)
{
IsInShip = true;
KeyDown = true;
camera.parent = ship.transform;
camera.localRotation = Quaternion.Euler(90f, 0f, 0f);
camera.localPosition = new Vector3(0f, 0f, 0f);
}
if (Input.GetKeyUp("e"))
{
KeyDown = false;
}
Answer by Llama_w_2Ls · Jul 30, 2020 at 09:00 AM
public Camera _camera;
public GameObject player;
private void Start()
{
_camera.transform.SetParent(player.transform);
}
You should really be using the SetParent() function, but what is the issue? In your P.S. you said that you already got the camera to parent to the player, right?
Hello, thanks for your answer and yes, I managed to get the camera to parent the player, but it still won't parent back to the player. Also I changed the code up a little bit. Here is the full code.
public Transform sensor;
public Transform enterIndicator;
public Transform player;
public Transform ship;
public Transform _cameraCenter;
public Transform _camera;
public float SensorSensingDistance = 5f;
bool SeesShip = false;
bool IsInShip = false;
// Update is called once per frame
void Update()
{
RaycastHit hit;
Ray sensorRay = new Ray(sensor.position, transform.forward);
if (!IsInShip)
{
if (Physics.Raycast(sensorRay, out hit, SensorSensingDistance))
{
if (hit.collider.tag == "vehicle")
{
enterIndicator.position = hit.point + transform.forward * -1f;
enterIndicator.rotation = Quaternion.Euler(transform.right * -45) * player.rotation;
SeesShip = true;
}
}else
{
SeesShip = false;
}
}
if (Input.GetKeyDown("e") && IsInShip)
{
_cameraCenter.transform.SetParent(player);
_cameraCenter.localRotation = Quaternion.Euler(0f, 0f, 0f);
_cameraCenter.localPosition = new Vector3(0f, 0f, 0f);
_camera.localPosition = new Vector3(0f, 0.8f, 3.7f);
player.position = ship.position + ship.transform.right * 10f;
player.rotation = Quaternion.Euler(Vector3.up * 90f);
IsInShip = false;
}
if (Input.GetKeyDown("e") && SeesShip)
{
IsInShip = true;
_cameraCenter.transform.SetParent(ship);
_cameraCenter.localRotation = Quaternion.Euler(90f, 0f, 0f);
_cameraCenter.localPosition = new Vector3(0f, 0f, 0f);
_camera.localPosition = new Vector3(0f, 2f, -8f);
}
}
Sorry, quick fix: I got the camera to parent to the ship not the player.
Answer by Maggiethegsd · Jul 30, 2020 at 12:15 PM
Use the set parent function instead of straight up going
Camera.main.transform.parent = player.transform;
Do this instead :-
Camera.main.transform.SetParent(player.transform);
Follow this Question
Related Questions
Instantiate a copy without its children 0 Answers
Instantantiate relative to parents position 1 Answer
Problems with transform.parent in unity 4.6 2 Answers
Setparent function dont work properly 0 Answers
Lag on set transform.parent 0 Answers