1
\$\begingroup\$

I'm trying to lift the front wheel of my bike, and I got it, applying this:

GetComponent.<Rigidbody>().AddForceAtPosition(Vector3(0,300,0),
        Vector3(GetComponent.<Rigidbody>().position.x,
        (GetComponent.<Rigidbody>().position.y)+5,(GetComponent.<Rigidbody>().position.z)+10));

Works perfect, but only when the bike is on start position, when I turn it to different position the force is applied on incorrect axis.

How I can do this applying the force on local object and ALWAYS on the same position and up direction??

Thank you.

-- EDIT --

As usual, problem solved, thank you to this amazing site and amazing people. Here is the working code:

public var force : Vector3;
public var forcePosition : Transform;
private var rbody : Rigidbody;

(on Start function)
rbody = GetComponent<Rigidbody>();

(on update function)
if(Input.GetKey(KeyCode.F) && Input.GetKey(KeyCode.UpArrow) && curGear >0  && WheelR.isGrounded)
{ 
var setPositionForce = Vector3(forcePosition.position.x,forcePosition.position.y,
forcePosition.position.z);
rbody.AddForceAtPosition (force, setPositionForce);
}

Well, I added on if the "F" key just for test and is necessary to move the wheel and to move the wheel first gear is nedded, and minium, the rear wheel is on floor. The bike is stable when the wheel is going up and works perfectly on all directions.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

The problem is that you are applying an absolute offset. You need it to be relative to the bike's transform. Have a look at

transform.forward

Etc.

As a simple alternative, you could just create an empty object as a child of the bike, position it on the front wheel and add it as a Transform reference to your script. You can then just use the child object's position to add force.

public Vector3 force;
public Transform forcePosition;
private Rigidbody rbody;

private void Start()
{
    rbody = GetComponent<Rigidbody>();
}
public void TriggerForce()
{
    rbody.AddForceAtPosition(force, forcePosition.position);
}
\$\endgroup\$
3
  • \$\begingroup\$ OH YEAH!! it worked. I can't understand the official documentations of this Unity functions, are very very complicated for me, and the first option you said is confused for me...well, the second option is simply PERFECT, but i needed to add one more line, this is the final code and is just I need, I edited the first post with the code. A LOT OF THANKS!! \$\endgroup\$
    – Jordi4321
    Aug 15, 2016 at 18:02
  • \$\begingroup\$ In short, X, Y, Z represent world space. If your bike is facing down the positive z axis, then the front of the bike may be found by taking it's position and adding some distance value to its position.z. Once the bike turns, this is no longer the case. Suppose the bike turns 90 degrees to the right, now facing down the positive x axis. Adding to the bike's z is now taking you to the bike's left. transform.forward let's you always find the front of the bike, now matter it's rotation \$\endgroup\$
    – zcabjro
    Aug 15, 2016 at 18:04
  • \$\begingroup\$ Great explanation, I now understand a little more these things. Thank you !!! \$\endgroup\$
    – Jordi4321
    Aug 16, 2016 at 5:57
0
\$\begingroup\$

Try this.

public Rigidbody rig;
public float force = 5f;
public Vector3 forcePositionOffset;

void Start () {

    rig = GetComponent <Rigidbody> ();
}

void Update () {

    rig.AddForceAtPosition (Vector3.up*force,transform.position+forcePositionOffset);
}

I recommend using this method (OnDrawGizmos or OnDrawGizmosSelected) to help you visualize in the editor where the force will be applied

public float gizmoSize;
void OnDrawGizmos(){

    Gizmos.DrawWireSphere (transform.position+forcePositionOffset,gizmoSize);
}
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the answer, but seems is the same as mine. The force is applied good, but only works on start position. When the bike turns to right, left or back not works. In right case the force is applied sideways, not to up...is strange or I'm doing something bad...the point to apply the force is perfect at start, but, then, when the bike is moving, the point moves to another point on the bike....localpositio‌​n is another option but I don't understand how to use it. \$\endgroup\$
    – Jordi4321
    Aug 15, 2016 at 14:40
  • \$\begingroup\$ This is all but identical to the question supplied code, bar some refactorisation. And you probably don't want to add force in Update. See FixedUpdate for more info \$\endgroup\$
    – zcabjro
    Aug 15, 2016 at 15:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .