Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Dec 11, 2020 at 06:08 PM by Nexoan for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Nexoan · Jul 30, 2020 at 08:47 AM · transform.parent

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;
         }
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

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?

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nexoan · Aug 10, 2020 at 08:49 AM 0
Share

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);
         }
     }

avatar image Nexoan · Aug 10, 2020 at 08:52 AM 0
Share

Sorry, quick fix: I got the camera to parent to the ship not the player.

avatar image
1

Answer by Maggiethegsd · Jul 30, 2020 at 12:15 PM

@Nexoan

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);
 
 



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges