简体   繁体   中英

Unity - Why is my ball rolling in the opposite direction when a projectile hits it?

For some odd reason, whenever my rigidbody ball is rolling AWAY from me, and I "shoot it" with a rigidbody projectile, the ball changes direction and moves toward me, as opposed to away from me. Physics are working fine otherwise (if the ball is rolling towards the playerobject when shot with the projectile, the impact of the projectile bounces/knocks the ball away in a correct direction/physics way.)

My bounce code is attached, I use it to bounce the ball off walls/etc and it is working perfectly. I suppose its because the ball is always moving towards a wall when it strikes it. I'm stumped though as to why when I shoot the ball as its rolling away from me it changes direction immediately. I've tried different mass, drags, physics materials, gravity - everything I can think of to no avail.

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

public class BounceBall : MonoBehaviour
{
    private Rigidbody rb;
    public float bounceForce = 6f;
    Vector3 lastVelocity;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void LateUpdate()
    {
        lastVelocity = rb.velocity;
    }

    private void OnCollisionEnter(Collision collision)
    {
        GameObject hitObject = collision.gameObject;

        if (hitObject.tag == "Pusher")
        {
            float speed = lastVelocity.magnitude;
            Vector3 direction = Vector3.Reflect(lastVelocity.normalized, collision.contacts[0].normal);
            rb.velocity = direction * Mathf.Max(speed, bounceForce * 3.5f);
        }
        else if (hitObject.tag == "Untagged")
        {
            float speed = lastVelocity.magnitude;
            Vector3 direction = Vector3.Reflect(lastVelocity.normalized, collision.contacts[0].normal);
            rb.velocity = direction * Mathf.Max(speed, bounceForce);
        }
    }
}

And the projectile code (don't think this has anything to do with it)

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

public class Projectile : MonoBehaviour
{
    public GameObject projectile;
    [SerializeField] float projVel = 50f;
    [SerializeField] float projLifeTime = 3.5f;
    [SerializeField] float projTimer;
    private Rigidbody rb;

    void Start()
    {
        rb = projectile.GetComponent<Rigidbody>();
        projTimer = projLifeTime;
        //rb.AddForce(0,projVel,0, ForceMode.Impulse);
        rb.AddForce(transform.forward * projVel, ForceMode.Impulse);
    }

    private void Update()
    {
        projTimer -= Time.deltaTime;
        if(projTimer <= 0f)
        {
            Destroy(gameObject);
        }
    }

The problem is the method Vector3.Reflect() . If you put in a backwards velocity it will output a forward velocity. The method Vector3.Reflect() is meant for an object bouncing off a surface or ricochets.

What you want to do is use Rigidbody.AddForce()

Here's the documentation: https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM