I have a player movement script in my game and a key aspect of the game is jumping however you cant because my script does not seem to work, I have a screenshot showing what may be a problem and also the script
sing 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 jumpForce = 100f;
public Transform groundCheck;
public float groundDistance;
public LayerMask groundMask;
public Rigidbody rb;
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);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (Input.GetKeyDown("space"))
{
rb.AddForce(0, jumpForce, 0, ForceMode.Impulse);
}
}
}
![alt text][1]
[1]: /storage/temp/151885-capsule.png
↧