using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(Rigidbody))]
public class Mover : MonoBehaviour
{
public float velocidadMovimiento = 50f;
public float velocidadRotacion = 600.0f;
private Animator anim;
public float x, y;
public string InputButton = "Jump";//Ahora puedes acceder desde el editor y simplemente cambiar el input a tu gusto
public string EjeX = "Horizontal";
public string EjeY = "Vertical";
Rigidbody Rb;
public float Empuje = 1500f;//Potencia del salto
public float ChekeoDeDistanciaAlSuelo = 0.5f;
public float Compensacion = 0.5f;//desde donde parte el rayo
public bool EstaEnElSuelo; //esta en el suelo o no
void Start()
{
//Obtiene los Componentes
anim = GetComponent<Animator>();
Rb = GetComponent<Rigidbody>();
}
void Update()
{
//Checkea distancia al suelo
DistanciaAlSuelo();
//llama a la funcion MoveCharacter
MoverJugador();
//llama a la funcion Jumping
if (Input.GetButtonDown(InputButton) && EstaEnElSuelo)
{
SaltarJugador();
}
}
private void DistanciaAlSuelo() //Checkea distancia al suelo
{
Ray ray;
ray = new Ray(transform.position - new Vector3(0, Compensacion, 0), -transform.up* ChekeoDeDistanciaAlSuelo);
if (Physics.Raycast(ray, ChekeoDeDistanciaAlSuelo))
{
NoJump();
}
else
{
IsJump();
}
}
private void SaltarJugador()//salto de jugador
{
Rb.AddForce(transform.up * Empuje);//agrega una fuerza en el el eje y , y si te vas moviendo salta hacia adelante
anim.SetBool("Jump",true);
}
private void MoverJugador(float x =0,float y=0)//mueve al jugador
{
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
anim.SetFloat("VelX", x);
anim.SetFloat("VelY", y);
//Esto traslada al player en los ejes
Vector3 Movimiento = new Vector3(x, 0.0f,y);
Rb.AddForce(Movimiento * velocidadMovimiento);
// Esto Rota al player al ir a otra direccion
if (Movimiento != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Movimiento.normalized), 0.2f);
}
}
void NoJump()
{
EstaEnElSuelo = true;
anim.SetBool("Jump", false);
}
void IsJump()
{
EstaEnElSuelo = false;
}
private void OnDrawGizmos()//Dibuja el rayo en el editor
{
Debug.DrawRay(transform.position - new Vector3(0, Compensacion, 0), -transform.up * ChekeoDeDistanciaAlSuelo, Color.magenta);
}
}
Good afternoon I have a problem I am creating my character applying basic movements such as jumping and running but I already managed to jump but when I give it play it jumps infinitely it should not be that way what I want is that when I press the spacebar the character jumps normally since at running would follow your direction up. can you help me with the problem please
4843553–465482–Mover.cs (2.89 KB)