AnswerBun.com

My bullets won't bounce in Unity?

Game Development Asked by kayra yorulmaz on November 6, 2021

I wanted bullets to bounce when they hit walls. However when colliding with a wall, they just jitter about on them. I didn’t want to create a new physics material as I want to keep the ball moving at a constant speed and to have them destroy themselves after they have bounced once. Thus, I wrote everything in Code.

The constraints for the bullet rigidbody’s freezing position is the Y axis while the freezing rotation is X, Y, and Z axis.

I also wondered if the initial position the instantiated bullet also had something to do with the speed the bullet travels:

GameObject bulletObject = Instantiate(bulletPrefab);
bulletObject.transform.position = transform.position + (transform.forward * -1);

Here’s the minimum reproducible code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class bulletMovement : MonoBehaviour
{
    private Rigidbody bulletRB;
    private Vector3 lastVelocity, initialVelocity;
    public float bulletSpeed = 10f;
    private int bounceCount;
    // Start is called before the first frame update
    private void Start()
    {
        bulletRB = GetComponent<Rigidbody>();
        bounceCount = 0;
        bulletRB.AddForce((transform.forward* -1) * bulletSpeed);
    }

    void Update()
    {
        lastVelocity = bulletRB.velocity;
    }

    // Update is called once per frame

    private void OnCollisionEnter(Collision col)
    {
        if (col.collider.tag == "Walls")
        {
            bounceCount += 1;
            if (bounceCount < 2)
            {   
                Bounce(col.contacts[0].normal);
            }
            else
            {
                Destroy(gameObject);
            }
        }
    }

    private void Bounce(Vector3 normal)
    {
        var speed = lastVelocity.magnitude;
        var direction = Vector3.Reflect(lastVelocity.normalized, normal);

        Debug.Log("Out Direction: " + direction);
        bulletRB.velocity = direction * Mathf.Max(speed, bulletSpeed);
    }
}

lastVelocity = bulletRB.velocity; is required to be in Update or else the bullet won’t move at all.

One Answer

You are moving your bullets by changing their transform.position in Update(). That way you are overriding the Unity physics engine. That means you can no longer expect it to do its job properly

If you want bullets to behave like ballistic projectiles with realistic physics, then you need to let the physics engine do its job instead of doing it yourself.

That means setting the initial velocity (or even better, set a velocity with rigidbody.AddForce(transform.forward * shootingForce, ForceMode.Impulse)). If your shooting force is large enough and your rigidbody settings like mass and friction are correct, then the projectile should move on its own without you having to do anything in Update or FixedUpdate and will behave physically correct on collisions (bounce off and transfer an impulse on the other rigidbody).

Answered by Philipp on November 6, 2021

Add your own answers!

Related Questions

Compute shader: Property at kernel index is not set

1  Asked on November 2, 2021 by glen-pierce

   

How can I cast light onto a sprite in 3D environment?

1  Asked on November 2, 2021 by vern-rowan

   

How to run skactions randomly

0  Asked on November 2, 2021 by why25

   

AntiGravity Vehicle?

1  Asked on November 2, 2021 by roswell

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir