Rigid body add force at position
So basicly my game is a 2d birds eye view space game where every object can only move on the x and z axis. The player can fire projectiles at asteroids and i was trying to make an AddForceAtPosition effect when the asteroid is hit to simulate a little more physics realism. I looked at the reference and had a few tries but my code didnt work. I didnt know how to apply the function into my collider code on line 3 because im a noob at functions.
void OnTriggerEnter ( Collider collider){
if(collider.gameObject.CompareTag ("Projectile"))
ApplyForce();
health -= 1;
if(health == 0){
health = 6;
transform.position = new Vector3(Random.Range (-25.0f,25.0f),0,Random.Range (-25.0f,25.0f));
}
}
void ApplyForce(Rigidbody body){
Vector3 direction = body.transform.position - transform.position;
body.AddForceAtPosition(direction.normalized, transform.position);
}
}
What's wrong wrong with it? Errors? Doesn't move at all (and the old version did)? Flies off the wrong way?
I'd cut the AddForce function. Esp. since you write that you don't know functions very well. Just copy those two lines back to OnTriggerEnter. It will have the same problems (whatewver they are,) but will be simpler.
Sorry i forgot to say that when i fire a projectile at the asteroid it does not add force to the position. I also tried deleting the function and adding the code to collider function but that did not work either.
void Update () {
transform.position = new Vector3(transform.position.x,0,transform.position.z); //$$anonymous$$eeps the game objects movement 2d by stopping y movement
transform.Rotate (asteroidRotation * Random.value,0,asteroidRotation * Random.value);
transform.Translate (movement * Random.value * Time.deltaTime,0,movement * Random.value * Time.deltaTime);
}
void OnTriggerEnter ( Collider collider){
if(collider.gameObject.CompareTag ("Projectile")){
Rigidbody body = transform.rigidbody;
Vector3 direction = body.transform.position -transform.position;
body.AddForceAtPosition(direction.normalized, transform.position);
health -= 1;
}
if(health == 0){
health = 6;
transform.position = new Vector3(Random.Range (-25.0f,25.0f),0,Random.Range (-25.0f,25.0f));
}
}
}
Answer by Owen-Reynolds · Oct 08, 2013 at 07:17 PM
"does not add force to the position": But what's wrong with it? Does it add no force at all? Or is it no change from normal AddForce?
For a test, shoot the edge of a wide object. Adding at the position should start the object spinning, and moving at a diagonal from the bullet (like a tricky pool ball shot.)
One obvious problem: you're using transform.position
, which is the center of the object. The whole point is to use where you hit it (AddForceAtPosition using the object center works, but is the same as regular add force.) I think you need to use OnCollisionEnter to even look up where you hit, from collision
. Triggers don't give you that extra info.
Your answer
Follow this Question
Related Questions
Rigidbody.addForce is not working 10 Answers
More problems with arrow shooting 0 Answers
Help with 2D topdown knockback 0 Answers
A 2D Character Controller with Rigidbody2D.AddForce 2 Answers