I am trying to make a game for myself and some friends just to mess around in and make better overtime when we all get better at coding and stuff.
my problem now is that my character can jump walk and look around but it cant jump when it is standing still.
i guess there is a problem with my gravity somewhere but i have not figured it out( that is why i am here)
i hope some of you can help me figure it out and explain what i did wrong so i can learn from the mistake.
thanks in advance
CS CODE (so you dont have to download the files)
PlayerController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public CharacterController controller;
private float verticalVelocity;
private float gravity = 14f;
private float jumpForce = 10.0f;
public float speed = 20f;
public Rigidbody rb;
public bool isGrounded;
private void Start()
{
controller = GetComponent();
rb = GetComponent();
}
private void Update()
{
Debug.Log(verticalVelocity);
Vector3 moveVector = Vector3.zero;
moveVector.x = Input.GetAxis(“Horizontal”) * speed;
moveVector.y = verticalVelocity;
moveVector.z = Input.GetAxis(“Vertical”) * speed;
controller.Move(moveVector * Time.deltaTime);
Vector3 move = transform.right * moveVector.x + transform.forward * moveVector.z;
controller.Move(move * speed * Time.deltaTime);
if (controller.isGrounded)
{
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetKey(KeyCode.Space))
{
verticalVelocity = jumpForce;
rb.AddForce(transform.TransformDirection(Vector3.up) * jumpForce, ForceMode.Impulse);
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
if (verticalVelocity == 0 && Input.GetKey(KeyCode.Space))
{
rb.AddForce(transform.TransformDirection(Vector3.up) * jumpForce, ForceMode.Impulse);
}
}
}
MOUSELOOK SCRIPT
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis(“Mouse X”) * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis(“Mouse Y”) * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -80f, 80f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
6406968–715197–PlayerController.cs (1.71 KB)
6406968–715200–MouseLook.cs (848 Bytes)