- Home /
add force on topdown bullet
I'm making a topdown game where you can shoot bullets that are bouncing of the wall. The player can Instantiate the bullets at his position. The bullets won't move, here is the script that's attached to the bullets:
private Rigidbody2D rb;
public float ballforce;
public GameObject player;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
transform.Rotate(0,0,player.transform.rotation.z);
}
// Update is called once per frame
void FixedUpdate () {
rb.AddForce(player.transform.forward * ballforce);
}
I'm trying to create something like this: https://www.twoplayergames.org/play/1373-Tank_Trouble_2.html I also have a physics material attached to the ball with bounce: 1.
Answer by giveson · Dec 30, 2018 at 09:41 PM
Try using a different method instead of using rb.addforce use public transform bulletstransform
void shoot() // whatever method you use { bulletstransform.translate(0, 0 , 0) // x y and z }
Thanks for your reply but I already fixed it. I've tried transform.translate before but then it won't bounce of the wall. I've fixed it with "Vector3 shootDir = transform.position - tank.transform.position;" and then "rb.AddForce(shootDir * ballforce);". Anyway thanks for your answer.