Comme l'indique le titre, je cherche à programmer un saut pour le fps-controler que je suis en train de programmer.
Pour l'instant, le joueur peut marcher en avant, en arrière, droite,gauche, et en diagonale.
Il peut courir, et j'ai ajouté des bruitages de pas.
J'ai programmé le contrôle de la caméra, avec une limite verticale réglable pour éviter de donner au joueur l'impression de "se casser le cou"...
Par contre, pour le saut, je suis bloqué.
J'ai tenté plusieurs chose : un AddForce au Rigidbody, mais ça saute moins haut que on se déplace...
J'ai tenté de changé directement la vitesse avec "GetComponent<Rigidbody> ().velocity", mais là le joueur arrête son déplacement horizontal lors du saut....
Je ne sais pas comment m'y prendre...
Voici mon code actuel :
Code : Tout sélectionner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControlPlayer : MonoBehaviour {
public float speed ;
public float runSpeed ;
public float jumpHigh;
public float MouseX;
public float sensivityX;
public int sensivityY ;
public float MouseY;
public float maxY ;
public float minY ;
public float velocityZ;
public float velocityY;
public float velocityX;
public Vector3 velocity;
public bool isRunning;
public bool hasJump ;
public int jumpTime;
public int jumpTimeok = 20;
public float runing ;
private float delfoDrag ;
private float delfoAngularDrag ;
public Quaternion MouseLook;
public Quaternion GravityAlign;
public GameObject MainCamera;
public GameObject jumpTester ;
// Use this for initialization
void Start () {
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
//Note actual drag and angular drag
delfoDrag = this.GetComponent<Rigidbody> ().drag;
delfoAngularDrag = this.GetComponent<Rigidbody> ().drag;
if (runSpeed == 0) {
runSpeed = 1;
}
}
// Update is called once per frame
void Update () {
//Note actual velocity
if (!jumpTester.GetComponent<JumpTester>().isJumping) {
velocityZ = this.GetComponent<Rigidbody> ().velocity.z;
velocityY = this.GetComponent<Rigidbody> ().velocity.y;
velocityX = this.GetComponent<Rigidbody> ().velocity.x;
velocity = this.GetComponent<Rigidbody> ().velocity;
}
//Look
//Right/Left
MouseX = Input.GetAxis ("Mouse X");
this.transform.Rotate (0,MouseX * sensivityX * 1,0);
//Up/Down
MouseY = Input.GetAxis ("Mouse Y");
if (MainCamera.transform.localRotation.x < maxY && MainCamera.transform.localRotation.x > minY) {
MainCamera.transform.Rotate (MouseY * sensivityY * -1, 0, 0);
}
else if (MainCamera.transform.localRotation.x >= maxY){
MainCamera.transform.Rotate (-1, 0, 0);
}
else if (MainCamera.transform.localRotation.x <= minY){
MainCamera.transform.Rotate (1, 0, 0);
}
//Test if player is running
if (Input.GetButton ("Run") == true) {
runing = runSpeed;
isRunning = true;
}
else {
runing = 1;
isRunning = false;
}
//Move
//Move Forward
if (Input.GetButton("Forward") == true && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = this.transform.forward * (speed*runing);
}
//Move Backward
if (Input.GetButton("Backward") == true && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = -this.transform.forward * (speed*runing);
}
//Move Right
if (Input.GetButton("Right") == true && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = this.transform.right * (speed*runing);
}
//Move Left
if (Input.GetButton("Left") == true && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = -this.transform.right * (speed*runing);
}
//Move diagonal Forward/Right
if ( (Input.GetButton("Forward") == true && Input.GetButton("Right") == true) && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = (this.transform.forward + this.transform.right) * (speed/1.5f*runing);
}
//Move diagonal Forward/Left
if ( (Input.GetButton("Forward") == true && Input.GetButton("Left") == true) && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = (this.transform.forward + -this.transform.right) * (speed/1.5f*runing);
}
//Move diagonal Backward/Right
if ( (Input.GetButton("Backward") == true && Input.GetButton("Right") == true) && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = (-this.transform.forward + this.transform.right) * (speed/1.5f*runing);
}
//Move diagonal Backward/Left
if ( (Input.GetButton("Backward") == true && Input.GetButton("Left") == true) && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = (-this.transform.forward + -this.transform.right) * (speed/1.5f*runing);
}
// Stop moving if release the button
if (Input.GetButtonUp ("Forward") == true && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = Vector3.zero ;
}
if (Input.GetButtonUp ("Backward") == true && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = Vector3.zero ;
}
if (Input.GetButtonUp ("Right") == true && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = Vector3.zero ;
}
if (Input.GetButtonUp ("Left") == true && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
this.GetComponent<Rigidbody> ().velocity = Vector3.zero ;
}
//Jump
if (Input.GetButton("Jump") && !jumpTester.GetComponent<JumpTester>().isJumping && !hasJump) {
Vector3 jump = new Vector3 (this.GetComponent<Rigidbody> ().velocity.x , (this.GetComponent<Rigidbody> ().velocity.normalized.y * jumpHigh) , this.GetComponent<Rigidbody> ().velocity.z) ;
this.GetComponent<Rigidbody> ().velocity = jump ;
hasJump = true ;
}
//stabilize
float X = jumpTester.GetComponent<JumpTester>().GravityAlignX ;
float Z = jumpTester.GetComponent<JumpTester> ().GravityAlignZ;
transform.rotation = Quaternion.Euler(X,this.transform.rotation.eulerAngles.y,Z);
}
}
(Certaines parties du code sont non-utilisées, mais me serviront sûrement plus tard).
Avec ce code, le saut ne s'effectue que lorsqu'on ne bouge pas....
Comment devrais-je m'y prendre ?
Merci d'avance .