0
\$\begingroup\$

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.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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).

\$\endgroup\$
6
  • \$\begingroup\$ Changing transform.position did fix the problem of bullets being stuck to the walls, but for some reason now the bullets bounce back onto the same wall in an arc and the bullets accelerate instead of immediately flying out. \$\endgroup\$ Commented Jul 21, 2020 at 13:21
  • \$\begingroup\$ @kayrayorulmaz Are you still having any code in Update or FixedUpdate? For proper ballistic physics, you shouldn't. The AddForce should happen once when the bullet is instantiated. And then you do not touch the velocity or the position while the bullet is flying. \$\endgroup\$
    – Philipp
    Commented Jul 21, 2020 at 13:26
  • \$\begingroup\$ I moved all of the code (which is 'lastVelocity = bulletRB.velocity;' and 'bulletRB.AddForce(transform.forward * bulletSpeed);' ) to the Start() void so that the speed wouldn't be affected every frame. However, now the bullets are moving too slowly no matter how much I change bulletSpeed. \$\endgroup\$ Commented Jul 21, 2020 at 13:34
  • \$\begingroup\$ @kayrayorulmaz Did you try lowering the mass and friction in the rigidbody of your bullets? \$\endgroup\$
    – Philipp
    Commented Jul 21, 2020 at 13:50
  • \$\begingroup\$ I decreased the mass to 1e-7 and increased bulletSpeed by 1000, and even then the speed didn't change. \$\endgroup\$ Commented Jul 21, 2020 at 14:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .