I have this script attached to a Sphere with a Rigidbody:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody rb;
public float forceStrenght = 10;
public bool isGrounded;
public float bounceStrenght = 3;
// Use this for initialization
void Start ()
{
rb = GetComponent ();
}
// Update is called once per frame
void Update ()
{
if (Physics.Raycast (transform.position, new Vector3 (transform.position.x, transform.position.y - .1f, transform.position.z), 1f)) {
isGrounded = true;
} else {
isGrounded = false;
}
if (Input.GetMouseButtonDown (0)) {
rb.AddForce (0, -forceStrenght, 0, ForceMode.VelocityChange);
}
if (isGrounded) {
rb.AddForce (0, bounceStrenght, 0, ForceMode.VelocityChange);
}
}
}
My problem is that sometimes the object bounces bigger sometimes smaller.
I don't want that, I want the object always reach the same height if I don't interupt it by pressing the LMB.
↧