• 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 /
avatar image
0
Question by KingHL · Jun 17 at 06:01 PM · cameratransformchildmirror3rd person controller

How to transform a child's position and rotation independently from their parent?

Hello!

I'm creating a 3rd person multiplayer game using Mirror and every single tutorial I've seen requires the camera to be parented under the player object's prefab.

This is infuriating because my entire player movement and rotation is based on the way the camera is facing.

 private void HandleRotation()
 {
     Vector3 targetDirection = Vector3.zero;
     
     targetDirection = cameraManager.cameraTransform.forward * inputHandler.verticalInput;
     targetDirection += cameraManager.cameraTransform.right * inputHandler.horizontalInput;
     targetDirection.Normalize();
     targetDirection.y = 0;
     
     if (targetDirection == Vector3.zero)
     {
         // keep our rotation facing the way we stopped
         targetDirection = transform.forward;
     }
     
     Quaternion targetRotation = Quaternion.LookRotation(targetDirection);
     Quaternion playerRotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);

     transform.rotation = playerRotation;
 }

For example: If I'm in game and looking to the right of my player model and then press 'w' to walk in the direction the camera is facing, my camera will spin with the player keeping the same rotation while my player spins in order to try and walk in the direction the camera is facing.

Ideally, I would like to find a solution that doesn't require me to parent the camera to the player model, however I can't find one, so instead I'm looking to be able to move the camera's transform independently to the player.

This is how I handle my camera rotation:

 private void RotateCamera()
 {
     Vector3 rotation;
     Quaternion targetRotation;

     // lookAngle = left and right input
     lookAngle += (inputHandler.cameraHorizontalInput * cameraLookSpeed);
     // pivotAngle = up and down input
     pivotAngle -= (inputHandler.cameraVerticalInput * cameraPivotSpeed);
     // limit rotation so we can't look in the ground etc.
     pivotAngle = Mathf.Clamp(pivotAngle, minimumPivotAngle, maximumPivotAngle);


     // Setting the rotation for y axis
     rotation = Vector3.zero;
     rotation.y = lookAngle;
     targetRotation = Quaternion.Euler(rotation);
     cameraHolderTransform.localRotation = targetRotation;

     // Setting the rotation for x axis
     rotation = Vector3.zero;
     rotation.x = pivotAngle;
     targetRotation = Quaternion.Euler(rotation);
     cameraPivotTransform.localRotation = targetRotation;
 }

It's worth noting as well that I am using 2 different gameObjects parented underneath the player object as a sort of camera holder for individual axis' (One of which is a parent and the other is a child). The camera gets parented to the child gameObject on Start.

Any help with this would be greatly appreciated! Thank you <3

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

· Add your reply
  • Sort: 
avatar image
0

Answer by TheTrueDuck · Jun 18 at 12:10 PM

Hi! It appears you are setting the transform.localRotation in your RotateCamera() method instead of using transform.rotation. This would explain why your camera is affected by your player's transform. Assuming your RotateCamera() method is called every update, that should be the only required change.

It seems weird that your camera has to be a child of your player. Are you sure it is required, or could those tutorials have just been using it as a shortcut? I'm afraid I haven't used Mirror, but most scenarios shouldn't require parenting in this way.

As for alternate solutions, you can Instantiate a gameobject in world space instead of local space, which should make the position of the parent not affect the instantiated gameobject.

 Instantiate(Object original, Transform parent, bool instantiateInWorldSpace);

Hope this helps!

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
avatar image
0

Answer by stalwart5033 · Jun 18 at 12:43 PM

you can set its parent to null so it will be parentless, transform.position provides parent-independent value, the transform.localPosition determines what position it is in relative to parent, same goes for rotation

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

237 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 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

How do I set a child object to not rotate if the parent WILL be rotating? 1 Answer

Expand script to stop camera from following player 2 Answers

Rotating gameobject after mouselook 0 Answers

transform.lookat() limitation... 1 Answer

Neutralising child rotations -> Parent 1 Answer


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