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 Buzzbomb · Jan 28, 2015 at 11:57 PM · c#rigidbodyparentaddforceatposition

AddForceAtPosition to Parent's rigidbody...C#

So I have a few gameobjects(I'll call them floatpoints) as children of a Boat which contains a rigidbody.

My intent is that when the float points (which are constantly raycasting upwards) get below a terrain which I have re-purposed into an "on trigger", shifting, perlin-noisey, wave-infused ocean they will apply upward force/torque to their parent's rigidbody at the position of the floatpoints themselves effectively simulating the realistic pitch/roll of a boat floating amongst waves.

Here is the code in it's entirety...

         using UnityEngine;
         using System.Collections;
         
         public class RealisticFloat : MonoBehaviour {
         
             public float hoverForce = 5.0F;
             public float hoverDamp = 0.5F;
             void Start() {
                 rigidbody.drag = 0.5F;
                 rigidbody.angularDrag = 0.5F;
             }
             void Update() {
                 int layerMask = 1 << 8;
                 
                 
                 RaycastHit hit;
                 Ray upRay = new Ray(transform.position, Vector3.up);
                 if (Physics.Raycast(upRay, out hit, 500f, layerMask)) {
                     float hoverError = hit.distance;
                     if (hoverError > 0) {
                         float upwardSpeed = rigidbody.velocity.y;
                         float lift = hoverError * hoverForce - upwardSpeed * hoverDamp;
                         transform.parent.rigidbody.AddForceAtPosition(lift * Vector3.up, transform.localPosition, ForceMode.Force);
                     }
                 }
             }
         }


...Now I know the majority of this code works as I've had objects float around when it is referring to itself and simply doing a...

 rigidbody.AddForce(lift * Vector3.up);

...so I think the culprit is in this line...

 transform.parent.rigidbody.AddForceAtPosition(lift * Vector3.up, transform.localPosition, ForceMode.Force);

...I think I am either not accessing the parent's rigidbody correctly or using "transform.localPosition" for my vector3 is not working as intended.

The code compiles fine but when I start my game my boat just falls through the "Ocean" without so much as a wobble (the lack of wobble tells me it isn't a matter of applying insufficient force as the terrain is shifting constantly on the update).

The scripts are within the floatpoints themselves fyi.

I am at a bit of a loss and have been scanning documentation and similar questions for days. If anyone could shed some light on what I am doing wrong I would be extremely grateful.

also sorry for being so wordy but details = clarity...

Comment
Add comment · Show 1
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 Buzzbomb · Jan 28, 2015 at 11:58 PM 0
Share

Okay I feel dumb...I got it working (kinda...) about a $$anonymous$$ute after submitting this question.

seems that I was defining the float "UpwardSpeed" I should have been referring to the parent's y velocity ins$$anonymous$$d of the floatpoint's y velocity.

Also on a humorous note the boat is spinning like crazy.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I access a collider's parent's rigidbody? 1 Answer

Making a haybale forkable 0 Answers

AddForce to position on Rigidbody 2 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