RigidBody.AddForce() doesn't apply
Hello, I'm working on a top down 2D shooter game. I'm currently working on the bullets, I've created a bullet prefab and attached a Rigidbody and a Bullet script to it. This is the code inside the bullet script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bullet : MonoBehaviour
{
public bool isEnemyBullet;
public float speed;
public Rigidbody2D rb;
private void FixedUpdate()
{
Move();
}
private void Move()
{
Debug.Log("Adding force");
rb.AddForce(transform.forward * speed);
}
}
It's a simple script, the problem is that the AddForce is only applied to the bullet once. When the bullet spawns in (I spawn in the bullets inside another script) it doesn't move. It just stays there, even though I'm calling Move() inside FixedUpdate.
The bullet's Rigidbody's body type is set to Kinematic and the Speed variable is set to 300. Any idea why my Bullet doesn't move?
Answer by Cornelis-de-Jager · Jul 29, 2019 at 09:16 PM
The bullet's Rigidbody's body type is set to Kinematic and the Speed variable is set to 300.
You can't apply Force to kenematic transforms.
Answer by SydiousMax · Jul 29, 2019 at 09:32 PM
Your code looks good. Change the prefab (of the bullets), and change the Rigidbody 2D from a "Kinematic" to "Dynamic". This should work. Change the gravity scale on the RigidBody from 1.0 to 0.0 and you should be good =)
For a deeper dive into the differences of static, kinematic, and dynamic. Here is the unity manual on the subject. https://docs.unity3d.com/$$anonymous$$anual/class-Rigidbody2D.html
Your answer
Follow this Question
Related Questions
Addforce for 2d game is not doing anything. 0 Answers
2D Knockback problem 2 Answers
Destorying objects on collsion 1 Answer
Why/How 2d tower of blocks collapse? 0 Answers
AddForce in Coroutine 1 Answer