- Home /
Separating direction from force w/ rigidbody.AddForce
Okay, so I have this code
var player : Transform;
function Update () { if (Input.GetButtonDown ("Jump")) { rigidbody.AddForce (transform.position - player.transform.position, ForceMode.Impulse); }
}
This finds the direction between my player and the object this code is attached to, then adds force in that direction. The problem is that the amount of force applied varies depending on my distance from the gameobject w/ the script.
So my question is, can I apply x amount of force in y direction, instead of having the force and direction combined into one variable?
Answer by Antony-Blackett · May 05, 2011 at 01:47 AM
var player : Transform; var force : float;
function Update () { if (Input.GetButtonDown ("Jump")) { rigidbody.AddForce ((transform.position - player.transform.position).normalized * force, ForceMode.Impulse); }
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613131701im_/https://answers.unity.com/themes/thub/images/avi.jpg)