- Home /
Bullet frozen and not moving
I'm making a top-down shooter and I was trying to code in how the bullet would add force to it when it is spawned in but when I tested it out the bullet would be frozen in the air unmoving. Here the whole script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FriendlyBulletSRP : MonoBehaviour
{
public float speed;
public GameObject destroyEffect;
public float lifetime;
public Rigidbody2D rb;
public
// Start is called before the first frame update
private void Start()
{
Invoke("DestroyBullet", lifetime);
}
// Update is called once per frame
public void Update()
{
rb.AddForce(transform.forward * speed);
}
void DestroyBullet()
{
Instantiate(destroyEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Any idea on how I can fix this?
Is speed
greater than 0
?
And by the way, use FixedUpdate
ins$$anonymous$$d of Update
to move a Rigidbody
The speed is greater than 0 and I changed the code to public void FixedUpdate() { rb.AddForce(transform.forward * speed); } Sadly the bullets still stay frozen
Also double check that the rigidbody on the bullet prefab is not set to kinematic = true.
The rigidbody was and is still set to dynamic with gravity at 0
Answer by NiallSharma · Jun 16, 2019 at 06:58 AM
I fixed it! Instead of using transform.forward I used transform.right and it worked!
Haha I had that thought because forward is z and the game is 2D. I should have said something, nice one!
Your answer
Follow this Question
Related Questions
Getting 2D projectiles that fire in the direction of the mouse position to work in unity 5? 0 Answers
Stop movement of rigid bodies 2D after collision 1 Answer
Character flies away at high speed with Rigidbody.AddForce 1 Answer
How to fire projectile in direction character is facing? 3 Answers
Rigidbody2D.velocity is not working. 0 Answers