Question by
ThatRatBailley · Aug 16, 2021 at 08:34 AM ·
new user
How do I make my player jump on blocks and not only the surface?
So I am new to unity, I don't know much but I think this has something to do with groundDistance. Anyways the problem is when I try to jump on block it doesn't work but when I am on the surface I can jump.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float gravity = -9.81f;
public float jumpHeight = 3f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Comment
Best Answer
Answer by ThatRatBailley · Aug 17, 2021 at 02:06 AM
Nevermind, I solved it, I just had to set a layer to every block and now it works perfectly.
Your answer
Follow this Question
Related Questions
Destroying Bullet (Impact Effect) 1 Answer
buttons interfere 0 Answers
Unity Will not open open a project 0 Answers