Hello I’m starting in unity and I’m actually trying to make a character controller and I’m trying to make a jump system but it’s not working. Can someone help me to solve my problem .This is the gravity script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerGravity : MonoBehaviour
{
[SerializeField]
bool isOnGround = false;
RaycastHit Hit;
[SerializeField]
CharacterController controller;
[SerializeField]
public float Gravity = -7;
[SerializeField]
float GravityMultiplier;
[SerializeField]
public float GravityVelocity;
public void FixedUpdate()
{
Debug.DrawRay(transform.position , transform.up*-1.2f, color:Color.red);
if(Physics.Raycast(transform.position, Vector3.down,out Hit, 1.2f))
{
if (Hit.collider.CompareTag("OnGround"))
{
isOnGround = true;
}
}
if (Hit.collider == null)
{
isOnGround = false;
}
if (isOnGround)
{
GravityVelocity = -1f;
}
if (!isOnGround)
{
GravityVelocity += Gravity * GravityMultiplier*Time.fixedDeltaTime;
}
controller.Move(new Vector3(0,GravityVelocity,0));
}
}
this is the player motor script:
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
public class PlayerMotor : MonoBehaviour
{
bool isOnGround = false;
RaycastHit Hit;
//recuperation du rb
[SerializeField]
public Rigidbody rb;
//recuperation du CharacterController
[SerializeField]
CharacterController controller;
//Variables move
[SerializeField]
float xMove;
[SerializeField]
float zMove;
//Variable pour récuperer la camera
[SerializeField]
Camera cam;
//création des variables velocity et rotate
public float jumpVelocity;
public Vector3 velocity;
public Vector3 xRotate;
public Vector3 yRotate;
//Récuperation de velocity dans Movement
public void move(Vector3 _velocity,float _jumpVelocity)
{
jumpVelocity = _jumpVelocity*Time.fixedDeltaTime;
velocity = _velocity;
}
//récuperation de xRotate et yRotate dans Movement
public void rotate(Vector3 _xRotate, Vector3 _yRotate)
{
xRotate = _xRotate;
yRotate = _yRotate;
}
void FixedUpdate()
{
if (Physics.Raycast(transform.position, Vector3.down, out Hit, 1.2f))
{
if (Hit.collider.CompareTag("OnGround"))
{
Debug.Log("OnGround");
isOnGround = true;
}
}
if (Hit.collider == null)
{
isOnGround = false;
}
jump();
//application des mouvements
controller.Move(transform.TransformDirection(velocity) * Time.fixedDeltaTime);
if (xRotate != null)
{
transform.Rotate(xRotate * Time.fixedDeltaTime);
}
if (yRotate != null)
{
cam.transform.Rotate(-yRotate * Time.fixedDeltaTime);
}
}
void jump()
{
if (jumpVelocity == 0) return;
if (!isOnGround) return;
controller.Move(new Vector3(0,jumpVelocity,0));
}
}
and this is the movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
//Variable des vitesses
[SerializeField]
float speed;
[SerializeField]
float mouseSensitivity;
[SerializeField]
float jumpForce;
//Variable pour accéder a PlayerMotor
public PlayerMotor playerMotor;
void FixedUpdate()
{
//Variables Inputs
float xInput = Input.GetAxisRaw("Horizontal");
float zInput = Input.GetAxisRaw("Vertical");
float yInput = Input.GetAxisRaw("Jump");
float xMouse = Input.GetAxisRaw("Mouse X");
float yMouse = Input.GetAxisRaw("Mouse Y");
//vecteur de la souris
Vector3 xRotate = new Vector3(0, xMouse * mouseSensitivity, 0);
Vector3 yRotate = new Vector3(yMouse * mouseSensitivity, 0, 0);
//calcule de la vélocité
Vector3 xMove = new Vector3(xInput, 0, 0);
Vector3 zMove = new Vector3(0, 0, zInput);
Vector3 yMove = new Vector3(0, yInput, 0);
Vector3 velocity = (xMove +zMove).normalized*speed;
Vector3 jumpVelocity = yMove.normalized *jumpForce;
//appel des fonction de PlayerMotor
playerMotor.move(velocity,jumpVelocity.y);
playerMotor.rotate(xRotate,yRotate);
}
}
I hope that someone will help me to solve the problem.