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 /
avatar image
0
Question by Zantom07 · Jul 12, 2012 at 09:35 PM · camerarotationrigidbodyparentchild

Child affecting parent rigidbody?

I have a GameObject hierarchy setup like this:

 - Player (Physics)
     - Player (Graphics)
     - Camera
         - 1stPersonGun

Now the physics part of the player is not a character controller, but a rigid body (because I require more than just the y axis of rotation). When I press 'w', the game should add a force to the rigidbody so that it moves towards wherever its forward is (it is a relative force of Vector3.forward).

The Physics is rotated along its y axis using mouse input, and the camera is rotated along its x axis also using mouse input.

My problem is, when the camera is looking down, the added force is very low, and when the camera looks up, the added force is very high. When I detach the camera from the hierarchy the force application behaves fine, so why does the camera being a child affect the parent rigidbody?

If I need to give any clarification, let me know - it was kind of hard to explain.

Here is my physics code:

 public Transform Player;
 
 void FixedUpdate () {
     Rigidbody mainPlayer = Player.rigidbody;

     if (Input.GetKey("w"))
     {
         mainPlayer.AddForce(Player.TransformDirection(Vector3.forward) * 20, ForceMode.Acceleration);
     }
     else if (Input.GetKey("s"))
     {
         mainPlayer.AddForce(Vector3.zero - (Player.TransformDirection(Vector3.forward) * 20), ForceMode.Acceleration);
     }
     else
     {
         if(_ContactPoints.Count > 0)
             mainPlayer.velocity = Vector3.zero;
     }
 }

Hierarchy picture

Comment
Add comment · Show 2
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 FishSpeaker · Jul 12, 2012 at 09:56 PM 0
Share

Does 1stPersonGun have a rigidbody associated with it?

Are you getting your relative forward vector based on the Camera's space at all?

avatar image Zantom07 · Jul 13, 2012 at 06:28 AM 0
Share

@FishSpeaker

No, it doesn't

And I don't think so. I am getting the player physics rigidbody, then doing AddRelativeForce(Vector3.forward, Force$$anonymous$$ode.Acceleration);

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by whydoidoit · Jul 13, 2012 at 07:26 AM

I'd guess we'd need to see the code for the physics - but as a guess without it - your 1stPersonGun has a collider attached to it. In which case the collider acts as a part of the parent rigidbody's physical structure and will alter the forces applied to it significantly. This would explain why different angles affected the force differently as the gun is changing its position as the camera moves and hence altering the shape of the object onto which you are applying physics.

Do one of these:

  • Don't have a collider on the gun (or the camera)

  • Keep the camera and gun separate

  • Extend CharacterController to give you the effects you want - very hard to do it perfectly with physics (but of course possible).

EDIT The final solution to this was to make sure that physics code refers to the transform.forward of the actual item with the rigidbody.

Comment
Add comment · Show 12 · 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 Zantom07 · Jul 13, 2012 at 03:03 PM 0
Share
  • I don't have a collider on anything but the 'Player (Physics)'

  • The gun isn't the problem. I have gone through a large amount of testing, and I rarely come to UnityAnswers unless I have tried everything I can think of. The camera is the problem.

  • I don't want to use CharacterController because it doesn't make any use of AddForce or AddRelativeForce, which I need for my game.

$$anonymous$$y physics code is now in the main question.

avatar image whydoidoit · Jul 13, 2012 at 03:08 PM 0
Share

So could you:

  • Post your physics code by editing the question

  • A screenshot of the hierarchy would be useful

avatar image Zantom07 · Jul 13, 2012 at 03:18 PM 0
Share

Ok I have added the physics code and hierarchy picture to the bottom of the question.

avatar image whydoidoit · Jul 13, 2012 at 03:26 PM 0
Share

Just a couple of things on that code (which isn't the problem by the look of things!):

  • Cache the rigidbody in Awake or Start it will be much faster

  • You can use Player.forward rather than calling TransformDirection(Vector3.forward)

Under $$anonymous$$usket - you are absolutely sure that there is nothing with a collider (nor does the camera have one?). Because that would totally explain it... Your physics works well

avatar image Zantom07 · Jul 13, 2012 at 03:37 PM 0
Share

Ok thanks for the 2 tips.

I just tried moving the camera out the hierarchy, and rotating it along the x axis still somehow affects where this 'Player.forward' is. The only thing that could possibly be affecting it is the fact that the physics code is on the camera and not on the player. I will try a few more things out - if you have any suggestions please tell me :)

Show more comments

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

parent object rotate with child camera 1 Answer

rigidbody on parent has to effect the child players but not the child camera's attatched to the players 0 Answers

Make a simple tree 1 Answer

Rigidbody constraints not working with parent/child relationship 2 Answers

How do I have a Child tell a Parent to move without the Child itself moving 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