Reputation: 53
Upon left clicking, the Player should jump if the condition of colliding with the "Ground" layer is true.
Problem: when I click it is not making the jump.
Hierarchy(3)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float jumpForce = 25f;
private Rigidbody2D rigidBody;
void Awake()
{
rigidBody = GetComponent<Rigidbody2D>();
}
void Update()
{
if (Input.GetMouseButtonDown(0)) {
Jump();
}
}
void Jump(){
if (IsGrounded()) {
rigidBody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
public LayerMask groundLayer;
bool IsGrounded()
{
if (Physics2D.Raycast(this.transform.position, Vector2.down, 0.2f, groundLayer.value)) {
return true;
}
else {
return false;
}
}
}
Upvotes: 0
Views: 220
Reputation: 111
I can't see how big your player is but make sure the raycast can actually reach the floor since it's only 0.2 meters long. You can use Debug.DrawLine()
to see the raycast in the editor.
Also make sure the jump force value is high enough.
Upvotes: 1