Je regarde les tutos de TUTO UNITY FR sur YouTube mais j'ai rencontré un problème que je n'arrive pas à régler: mon personnage ne veux pas sauter.
Voici le script en questio,
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class character_motor : MonoBehaviour {
// Animation du perso
Animation animations;
// Vitesse de déplacement
public float walkSpeed;
public float runSpeed;
public float turnSpeed;
// Inputs
public string inputFront;
public string inputBack;
public string inputLeft;
public string inputRight;
public string inputRun;
public string inputJump;
public Vector3 jumpSpeed;
CapsuleCollider playerCollider;
void Start () {
animations = gameObject.GetComponent<Animation>();
playerCollider = gameObject.GetComponent<CapsuleCollider>();
}
bool IsGrounded()
{
// Ici on vérifie s'il y a un sol sous le joueur
return Physics.CheckCapsule(playerCollider.bounds.center, new Vector3(playerCollider.bounds.center.x, playerCollider.bounds.min.y - 0.01f, playerCollider.bounds.center.z), 0.1007767f);
}
void Update () {
// Si on avance
if (Input.GetKey(inputFront) && !Input.GetKey(inputRun))
{
transform.Translate(0, 0, walkSpeed * Time.deltaTime);
animations.Play("walk");
}
// Si on recule
if (Input.GetKey(inputBack))
{
transform.Translate(0, 0, -(walkSpeed / 2) * Time.deltaTime);
animations.Play("walk");
}
// Rotation à gauche
if (Input.GetKey(inputLeft))
{
transform.Rotate(0, -turnSpeed * Time.deltaTime, 0);
}
// Rotation à droite
if (Input.GetKey(inputRight))
{
transform.Rotate(0, turnSpeed * Time.deltaTime, 0);
}
// Si on avance
if (Input.GetKey(inputFront) && Input.GetKey(inputRun))
{
transform.Translate(0, 0, runSpeed * Time.deltaTime);
animations.Play("run");
}
// Si on n'avance pas et qu'on ne recule pas
if (!Input.GetKey(inputFront) && !Input.GetKey(inputBack))
{
animations.Play("idle");
}
// Si on saute
if(Input.GetKeyDown(inputJump) && IsGrounded())
{
//Préparation du saut (Nécéssaire en C#)
Vector3 v = gameObject.GetComponent<Rigidbody>().velocity;
v.y = jumpSpeed.y;
// Saut
gameObject.GetComponent<Rigidbody>().velocity = jumpSpeed;
}
}
}
.