- Home /
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.
@Jonny_$$anonymous$$ Did you ever find the answer to this? I am having the exact same problem :(
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.
Your answer
Follow this Question
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