So, I’m creating a 3D first-person platformer. I’m trying to make the player, or any other RigidBody object, bounce off of a specific type of platform in a specified manner when they collide. I’ve tried a few different things, but no matter what I try, I can’t seem to get the Player to bounce like they’re supposed to (although other RigidBody objects are working just fine). How can I get the Player to bounce like the other objects?
Here’s the script I have right now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bounce : MonoBehaviour
{
public bool totalBounce = true;
public bool bounceX = false;
public bool bounceY = false;
public bool bounceZ = false;
public float powerX = 10.0f;
public float powerY = 10.0f;
public float powerZ = 10.0f;
void OnTriggerEnter (Collider other)
{
Debug.Log (other.name + " is in Trigger.");
Rigidbody body = other.GetComponent<Rigidbody> ();
Vector3 bodySpeed = new Vector3 (-body.velocity.x, -body.velocity.y, -body.velocity.z);
if (body != null) {
if (bounceX == true) {
BounceX (body);
}
if (bounceY == true) {
BounceY (body);
}
if (bounceZ == true) {
BounceZ (body);
}
if (totalBounce == true) {
body.AddForce (bodySpeed, ForceMode.Impulse);
}
}
}
void OnTriggerExit (Collider other)
{
Debug.Log (other.name + " has left Trigger.");
}
void BounceX (Rigidbody rb)
{
rb.AddForce (powerX, 0, 0, ForceMode.Impulse);
}
void BounceY (Rigidbody rb)
{
rb.AddForce (0, powerY, 0, ForceMode.Impulse);
}
void BounceZ (Rigidbody rb)
{
rb.AddForce (0, 0, powerZ, ForceMode.Impulse);
}
}