Hello, i’m trying to make a 3d character controller.
Everything is okay, except for the jumping. The character jumps normally if the game is not reading any other input, but if i try to jump while moving, the character does not jumps almost at all, or if i move while jumping it keeps floating.
How to fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody playerRB;
private float hInput;
private float vInput;
private Vector3 direction;
[SerializeField] private float speed;
[SerializeField] private float rotationSpeed;
[SerializeField] private float jumpForce;
[SerializeField] private Transform camDir;
[SerializeField] private bool grounded;
void Start()
{
playerRB = GetComponent<Rigidbody>();
}
void Update()
{
Movement();
Jump();
}
private void FixedUpdate()
{
}
void Movement()
{
//inputs
hInput = Input.GetAxisRaw("Horizontal");
vInput = Input.GetAxisRaw("Vertical");
direction = new Vector3(hInput,0, vInput).normalized;
//camera relations
Vector3 camForward = camDir.transform.forward;
Vector3 camRight = camDir.transform.right;
camForward.y = 0;
camRight.y = 0;
Vector3 forwardRelative = camForward * vInput;
Vector3 rightRelative = camRight * hInput;
Vector3 relativeDirection = forwardRelative + rightRelative;
if (direction.magnitude !=0)
{
Quaternion lookDirection = Quaternion.LookRotation(relativeDirection, Vector3.up);
//mueve al personaje
playerRB.velocity = new Vector3(relativeDirection.x,0, relativeDirection.z) * speed * Time.deltaTime;
//rota al personaje
transform.rotation = Quaternion.RotateTowards(transform.rotation, lookDirection, rotationSpeed * Time.deltaTime);
}
}
void Jump()
{
if(Input.GetButtonDown("Fire1") && grounded) {
playerRB.AddForce(playerRB.velocity.x, jumpForce, playerRB.velocity.z);
grounded = false;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Ground"))
{
grounded = true;
}
}
}
It helps to post your code. It's hard to imagine what your code is.
– ArachnidAnimal