- Home /
Why is force only being added in the same direction?
I am trying to build a boxer who is always looking at the player, and punches at the player when in range. The punching is being done with AddForce
, with the glove's forward as the direction. No matter what direction the boxer and glove face the glove moves in the same direction (0, 0, -1); This is the code I am using, it's on the boxer which is a parent of the glove.
gloveRB = glove.GetComponent.<Rigidbody>();
function Update() {
transform.LookAt(target.transform);
var HitInfo : RaycastHit;
var gloveDirection = glove.transform.InverseTransformDirection(gloveRB.velocity);
if (gloveDirection.magnitude < 0.1) {
if (Physics.Raycast(glove.transform.position, glove.transform.forward, HitInfo, 2)) {
if(HitInfo.transform.gameObject.tag == 'boxer') {
Debug.DrawRay(glove.transform.position, glove.transform.forward * 2, Color.blue, 20);
gloveRB.AddForce(glove.transform.forward * 10 * Time.deltaTime, ForceMode.Impulse);
}
}
}
It maybe worth noting that I have a spring joint connecting the glove and boxer so that the glove is drawn back after the punch. I am using ForceMode.Impulse
because I only want this force added all at once. The debug ray is drawn in the correct direction, and the glove should follow it but it just moves in (0, 0, -1)
Why isn't the force being added in the right direction?
Answer by Eno-Khaon · Apr 17, 2016 at 02:16 AM
It appears you simply need to replace
gloveRB.AddRelativeForce(...);
with
gloveRB.AddForce(...);
You're already accommodating the relative direction using glove.transform.forward
, so there's no need to get a relative direction.
Off the top of my head, I can't think of why the force would be added in exactly the direction it is, let alone exclusively, since I think the rotation should logically be doubling itself, but regardless, there should be no need for a relative force when you're already factoring in the relative angle.
That said, it you wanted to use AddRelativeForce anyway, that could be done by changing it to:
gloveRB.AddRelativeForce(Vector3.forward * 10, ForceMode.Impulse);
Sorry that was my typo. I had been using AddForce
, but before posting the question I tried AddRelativeForce
and forgot to change it back. Though I did try your suggestion for AddRelativeForce(Vector3.forward
which resulted in the glove moving towards the global forward.
Hmm... well, there are a few other things you can look at (including an oversight on my part).
First, you mention wanting the glove to launch forward with a full initial force all at once. That means you don't need to multiply by Time.deltaTime in an AddForce call. That said, you really shouldn't apply deltaTime regardless, since it's automatically applied by the two non-immediate Force$$anonymous$$ode choices.
That said, based on these two lines of yours, especially:
// Removed deltaTime
Debug.DrawRay(glove.transform.position, glove.transform.forward * 2, Color.blue, 20);
gloveRB.AddForce(glove.transform.forward * 10, Force$$anonymous$$ode.Impulse);
There is no good reason why the ray and the force application would go in different directions unless there are some additional factors preventing mobility which aren't readily apparent (such as any unexpected colliders blocking movement, rigidbody settings preventing motion, or spring joint adding unexpected restrictions).
Your answer
Follow this Question
Related Questions
Get result (force & torque) of AddForceAtPosition? 2 Answers
Add Force without Rotation 2 Answers
How can I convert velocity/direction to Force? 3 Answers
add force to object that has 2 different rigid bodies 0 Answers
Returning a rigidbody back to its original x and z rotations through physics forces. 2 Answers