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 Jonny_M · Dec 22, 2013 at 12:57 AM · gameobjectcharactercontroller

How to add Forces and Torques relative to an object at a none central position

I am new to unity and I have no idea how to add a force to an object at a position that is not (0,0,0) Relative to the object. When i started adding forces i used the function Ridgedbody.AddForce this just added a force that was relative to the game maps center location. I then used Ridgedbody.AddRelativeForce this gave the object a force but it was only able to be applied from the objects center. I have heard about AddForceAtPosition but i don't know how to apply it, and i think it might just give a force relative to a point on the map. Here is what i have so far for my object controller.

 using UnityEngine;
 using System.Collections;
 
 public class Driver : MonoBehaviour {
 
     // Use this for initialization
     void Start () {}
     
     // Update is called once per frame
     void Update () 
 
         // roll controls
 
         {if(Input.GetKey(KeyCode.W))
         {rigidbody.AddRelativeTorque(15, 0, 0);}
 
         if(Input.GetKey(KeyCode.S))
         {rigidbody.AddRelativeTorque(-15, 0, 0);}
 
         if(Input.GetKey(KeyCode.A))
         {rigidbody.AddRelativeTorque(0, 0, 10);}
 
         if(Input.GetKey(KeyCode.D))
         {rigidbody.AddRelativeTorque(0, 0, -10);}
 
         if(Input.GetKey(KeyCode.RightArrow))
         {rigidbody.AddRelativeTorque(0, 5,0);}
 
         if(Input.GetKey(KeyCode.LeftArrow))
         {rigidbody.AddRelativeTorque(0, -5,0);}
 
         // forward and backward controls
 
         if(Input.GetKey(KeyCode.UpArrow))
         {rigidbody.AddRelativeForce(0,0,10);}
 
         if(Input.GetKey(KeyCode.DownArrow))
         {rigidbody.AddRelativeForce(0, 0,-10);}
     }}

Im Ok with my torque forces being applied from the objects center but i want my regular forces to be applied 1.5 unity distance units below the objects center. So to be spesific i want a force with a magnitude of (0,0,10) applied from a transform location of (0,-1.5,0) relative to the objects center, and another force with a magnitude of (0,0,-10) applied from a transform location of (0,-1.5,0) relative to the objects center.

I would also like to know how to how to apply torques from a none central position relative to the object as there is a good chance i will have to do this later on.

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 kilobytelogic · Oct 26, 2015 at 04:59 PM 0
Share

@Jonny_$$anonymous$$ Did you ever find the answer to this? I am having the exact same problem :(

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kingcoyote · Oct 26, 2015 at 05:34 PM

You should be able to use RigidBody.AddForceAtPosition for this. I'm not sure why you had trouble with it. Maybe the nature of the position Vector3 was problematic?

The position Vector3 is in world space, so if you want to offset it from your object's position, you just need to do a little math You say you want the force to be offset by a certain amount, so try this:

 rigidbody.AddForceAtPosition(
     new Vector3(0, 0, -10), 
     transform.position + new Vector3(0, -1.5, 0)
 );

That will create a force with a magnitude of (0, 0, -10) and apply it just near your transform.position. If you don't offset it to your transform.position, it will be relative to the global (0, 0, 0), which is not what you want.

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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

C# Rotate GameObjects Regardless of List Size 2 Answers

A node in a childnode? 1 Answer

C# Change Script Help 1 Answer

C# SetActive GameObject Array 2 Answers

C# GameObject Reverses Z Rotation 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