Need help with adding ground detection to my jump. (c#) (3D)
As i wrote in the title i need with ground detection. I want to make it so that only can jump once and i know that you can use ground checking or raycast and i have tried both but i couldn´t get it to work.
My movement code: public class Boll_kontrollLVL3 : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
public float jumpSpeed = 100.0f;
private Rigidbody rb;
private int count;
float movementSpeed;
void Update()
{
float amountToMove = movementSpeed * Time.deltaTime;
Vector3 movement = (Input.GetAxis ("Horizontal") * -Vector3.left * amountToMove) + (Input.GetAxis ("Vertical") * Vector3.forward * amountToMove);
rb.AddForce (movement, ForceMode.Force);
if (Input.GetKeyDown ("space")) {
rb.AddForce (Vector3.up * jumpSpeed);
}
}
void Start ()
{
rb = GetComponent<Rigidbody> ();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up")) {
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Cubes Collected: " + count.ToString ();
if (count >= 12) {
winText.text = "Level cleared!";
}
if (count >= 12) {
Application.LoadLevel ("Roll a Ball 04");
}
}
}
Answer by supermegamestre · Dec 04, 2017 at 11:26 PM
helooo i'm BR so my english are NOT perfect sooooo... for some time i had the same problem buut... exits OnTriggerEnter right? so,exits also a OnCollisionEnter too XD well here is my ground detection system
void OnCollisionEnter(Collision any){
if (any.collider.tag == "MultiversalCollidingInertObject") {
onGround = true;
}
}
hope i was helpfull, byeeeeee
Your answer
Follow this Question
Related Questions
Jumping Function in my Player Controller Script isn't working 2 Answers
Hold the jump button to jump higher with CharacterController 1 Answer
How to make the character walk-jump and walk (and resize the collider when crouched) ? 0 Answers
While (starting jumping or in jump) holding W key makes game object jump high and fall slow. 1 Answer
How to double jump? i just can jump once when it touch the ground.. help me guys =D 1 Answer