- Home /
AddForce not moving object after instantiate
I'm trying to make a top down shooter and right now I'm getting the player to shoot. Basically I'm using the same line of code to move the projectile as I use to move the player, but it does not work. This is the code:
void FixedUpdate()
{
this.GetComponent<Rigidbody2D>().AddForce(gameObject.transform.up * projectileSpeed);
}
This projectileSpeed is set to more than zero and I've tested it with a fixed float value, still nothing. It just gets instantiated and stands still. Any idea of what's going on or what am I doing wrong? Thanks!
Try putting a Debug.Log inside the fixed update to check that the fixed update gets called properly.
Answer by Kriogenic · Apr 12, 2015 at 10:40 AM
You are probably better off using .velocity which is how I normally handle bullets.
this.GetComponent<Rigidbody2D>().velocity = gameObject.transform.up * projectileSpeed;
This way you can set the velocity to a nice value like 100 and get a nice bullet effect velocity does not need to be called in fixedupdate and can just be called once in start when the bullet is created.
Also if your working on a 2D game make sure transform.up is not making it move in the Z axis by checking the transform of the component while the game is running.
Your answer
Follow this Question
Related Questions
RigidBody2D Performance with AddForce on WakeUp 1 Answer
Rigidbody2d.addforce in the direction of mouse problem 1 Answer
Circular movement using AddForce() 1 Answer
Is it possible to have slow acceleration and high drag at the same time? 0 Answers
,Rotate a gameobject around another while being attracted by its gravity 1 Answer