I’m very new to unity, and started following the unity roll a ball tutorial, and wanted to expand my game by making the charachter jump. I managed to figure out how to do it, and now i want it to only be ale to jump while touching the tag “ground” i put together a script but its not working, can anybody help me with that?
This is my complete script.
The lines of the code where i try and let the ball only jump when touching the ground, starts with
void OnColliderEnter(Collider other)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using TMPro;
public class PlayerController : MonoBehaviour
{
public float speed = 20;
public float jumpForce = 7;
public TextMeshProUGUI countText;
public GameObject TextWinObject;
private Rigidbody rb;
private int count;
private float movementX;
private float movementY;
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
TextWinObject.SetActive(false);
}
void OnMove(InputValue movementValue)
{
Vector2 movementVector = movementValue.Get<Vector2>();
movementX = movementVector.x;
movementY = movementVector.y;
}
void OnColliderEnter(Collider other)
{
if (other.gameObject.CompareTag("Ground"))
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}
}
void FixedUpdate()
{
Vector3 movement = new Vector3(movementX, 0.0f, movementY);
movement = movement.normalized * speed;
rb.AddForce(movement);
}
void Update()
{
}
void SetCountText()
{
countText.text = "Coins count: " + count.ToString();
if (count >= 12)
{
TextWinObject.SetActive(true);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
}