2

I'm working on a simple 3D game where some balls (fixed Z position) fall along a path (using gravity and physics material) to a small flat platform and "power bounce" off this platform. The player can rotate this platform so I want to recreate a realistic bounce direction according to the platform's angle.

I'm new to coding but so far I've figured the relationship between the vector of the ball as it comes into collision with the platform and the platform's normal, which should be a perpendicular line from the surface and that can be used to reflect the ball's vector to the other direction.

I already used OnCollisionEnter and if statement to detect whether it's the platform you are colliding with, but I don't understand where to indicate the normal of the surface and how to access it. Should it be as a public class in the other object or can it be detected from the ball game object?

I tried some examples from this and other websites and got this far:

public class OnCollision : MonoBehaviour
{

    public float speed = 25f;
    public Rigidbody rb;
    private Rigidbody rigid;

    private void Start()
    {
        rigid = transform.GetComponent<Rigidbody>();
    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.transform.tag == "BouncePad") {
            rb.velocity = transform.up * speed;
        }

    }
}

Now it bounces off vertically, so I'm guessing I should change the code where the transform.up * speed part is.

Could anyone guide me, please?

Much appreciated.

2
  • Have you tried letting Unity physics handle this, or is that something you're trying to avoid?
    – Joe
    Aug 12, 2019 at 20:06
  • I'd like the ball to have a regular bounce from all the scene except from the platform at the bottom of the path which boosts the bounce, like a jump pad. This platform can rotate by user input.
    – diegodaly
    Aug 12, 2019 at 21:55

3 Answers 3

3

If you are already using Physics material, look into the Bounciness property. A value of 0 means no bounce, a value of 1 will lead to no loss of energy. The angle of the bounce will be calculated for you. Make sure you drag the physics material onto each object-- both the ball and the wall's material will have an effect.

3
  • Correct, the bounciness is at 0.8 now, but what I'd like to achieve is the platform at the bottom to behave like a jump pad and bounce of the ball in a powerful way. The platform can rotate by user input and I'd like to get the bounce angle right.
    – diegodaly
    Aug 12, 2019 at 21:57
  • Ah. In that case you could let the bounce happen and then use AddForce in the direction of the ball's velocity right after bouncing. You can use Vector3.normalized to get just the direction the ball is moving and then multiply that by your "power" number. A simpler solution would be to just multiply its velocity by a scalar when you collide but I think its recommended to use AddForce over changing velocities directly, when working with Unity Physics.
    – nscarfo
    Aug 12, 2019 at 22:05
  • Finally someone helped me out and it was with AddForce to the rb.velocity * strength float :)
    – diegodaly
    Aug 15, 2019 at 19:34
1

Finally somebody gave me a hand and came to this solution:

public class Bounce : MonoBehaviour
{
public Rigidbody rb;
public float str = 0.21f;
public float str2 = 0.15f;

// Start is called before the first frame update
private void Start()
{
    rb = GetComponent<Rigidbody>();
}

private void OnCollisionEnter(Collision col)
{
    if (col.gameObject.tag == "BouncePad")
    {
        rb.AddForce(rb.velocity * str, ForceMode.Impulse);
    }
    if (col.gameObject.tag == "BouncePad2")
    {
        rb.AddForce(rb.velocity * str2, ForceMode.Impulse);
    }
}

// Update is called once per frame
void Update()
{

}

}

0

public class BouncTest : MonoBehaviour {

[SerializeField] private float hight = 3;
[SerializeField] private int times = 5;
[SerializeField] private float speed = 8;

private Vector3 _startPos;
private bool _checkUP;
private int _countTimes;
private float _hightbuf;
[HideInInspector]
public bool _bounceEnd;

private void Awake()
{
    _startPos = transform.position;
}

public void TurnOnBounceEffect()
{
    _bounceEnd = true;
    _checkUP = false;
    _hightbuf = hight;
    _countTimes = 0;
}

private void FixedUpdate()
{
    BounceEffect();
}

private void BounceEffect()
{
    if (_bounceEnd)
    {
        if (!_checkUP)
        {
            if (transform.position.y <= (_startPos.y + _hightbuf))
                transform.position = Vector2.MoveTowards(transform.position, new Vector2(_startPos.x, transform.position.y) + (Vector2.up * _hightbuf), speed * Time.fixedDeltaTime);
            else
            {
                _checkUP = true;
            }
        }
        else if (times != _countTimes)
        {
            if (transform.position.y > _startPos.y)
                transform.position = Vector2.MoveTowards(transform.position, _startPos, speed * Time.fixedDeltaTime);
            else
            {
                _countTimes++;
                _checkUP = false;
                _hightbuf /= 2;
            }
        }
        else
        {
            transform.position = Vector2.MoveTowards(transform.position, _startPos, speed * Time.fixedDeltaTime);
            if (transform.position.y <= _startPos.y)
            {
                _bounceEnd = false;
            }
        }
    }
}

}

1
  • Hello and welcome to StackOverflow! Please provide more details as to what your code does, and why does it answer the question.
    – Ad5001
    Jul 30, 2021 at 13:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.