J'ai réussi à avoir un code qui à l'air de fonctionnouiller !
Je me déplace correctement et saute correctement. J'ai mis la gravité de la scène à -98.1, 10* plus que les paramètres par défaut...
Voilà le code:
Code : Tout sélectionner
// ===============================================
// Import modules
// ===============================================
using UnityEngine;
using System.Collections;
// ===============================================
// Character Control
// ===============================================
public class PlayerScript : MonoBehaviour {
// ===========================================
// Base variables
// ===========================================
public float moveSpeed = 30.0f;
public float turnSpeed = 4.0f;
public float jumpPower = 30.0f;
private Quaternion transformRotation;
private Vector3 moveVelocity;
private Rigidbody rb_transform;
private float yDeg;
private bool raycastReturn;
private bool isGrounded;
// ===========================================
// Event
// ===========================================
void Awake() {
rb_transform = transform.GetComponent<Rigidbody>();
yDeg = transform.rotation.eulerAngles.y;
}
void FixedUpdate() {
// Init variables
moveVelocity = Vector3.zero;
CheckGround ();
// Move
Turn();
Move();
}
// ===========================================
// Movement
// ===========================================
void Move()
{
// Transform velocity with input and RigidBody
moveVelocity = Vector3.ProjectOnPlane(transform.forward, Vector3.down);
moveVelocity *= Input.GetAxis ("Vertical") * moveSpeed;
moveVelocity = Vector3.Scale(rb_transform.velocity, new Vector3(0, 1, 0)) + Vector3.Scale(moveVelocity, new Vector3(1, 0, 1));
// Jump
if (Input.GetKeyDown ("space") && isGrounded) {
moveVelocity += Vector3.up * jumpPower;
}
// Set rigidBody
rb_transform.velocity = moveVelocity;
}
void Turn()
{
yDeg -= -Input.GetAxis("Horizontal") * turnSpeed;
transformRotation = Quaternion.Euler (0.0f, yDeg, 0.0f);
// Set rotation
transform.rotation = transformRotation;
}
// ===========================================
// Check
// ===========================================
void CheckGround()
{
RaycastHit hitInfo;
raycastReturn = Physics.Raycast (transform.position, Vector3.down, out hitInfo, 4.1f);
if (raycastReturn) {
isGrounded = true;
}
else {
isGrounded = false;
}
}
}
Je vais quand même essayer de faire un characterControler sans RigidBody aussi.