Bounce a ball with same height but faster movement
This is my problem: I have a bouncing ball, wich works fine, but I want it to bounce with double the speed. I use a combination of velocity and bouncing Material. I added the main part of my code:
using UnityEngine;
using UnityEngine.SceneManagement;
public class MoveSphere : MonoBehaviour {
public float _fallVelocity = 6.1f;
float fallVelocity;
int speedThrough;
Rigidbody rb;
private void Awake() {
speedThrough = 0;
fallVelocity = _fallVelocity;
rb = GetComponent<Rigidbody>();
}
private void Start() {
//Speed up the sphere
rb.velocity = Vector3.down * fallVelocity;
}
void LateUpdate() {
//Prevent to be too fast.
if (rb.velocity.y < -fallVelocity) {
rb.velocity = Vector3.down * fallVelocity;
}
}
private void OnCollisionEnter(Collision collision) {
speedThrough = 0;
fallVelocity = _fallVelocity;
//Prevent to high jump
rb.velocity = Vector3.up * fallVelocity;
}
private void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Plane") {
if (fallVelocity < _fallVelocity + 4 * 1.5f) {
fallVelocity += 1.5f;
}
}
else {
fallVelocity = _fallVelocity;
rb.velocity = Vector3.down * fallVelocity;
GetComponent<Collider>().isTrigger = false;
GetComponent<Renderer>().material.color = Color.white;
}
}
}
Comment