I created a terrain and i added my player prefab.
That done this :
My player collide with a slope, and the collision glitch.
Why?
Here’s my movement code:
#pragma strict
@Header("Statistics")
@Range(0,5)
var speed : float;
@Range(0,50)
var jumpForce : float;
var playerRigidbody : Rigidbody;
var inputAxis : Vector3;
var grounded : boolean;
@Header("OverlapSphere")
var groundSphereRadius : float;
var groundLayers : LayerMask;
var debugCollision : boolean;
var check : Vector3;
@Header("Debug")
var realSpeed : float;
var speedMultiplicator : float;
var smoothTime : float;
function FixedUpdate () {
var horizontalDivisor : float;
inputAxis = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
var realInputAxis : Vector3;
if(Input.GetButton("Run") && grounded){
speedMultiplicator = 2;
horizontalDivisor = 2;
}else if(Input.GetButton("Crouch") && grounded){
speedMultiplicator = 0.5;
horizontalDivisor = 1;
}else{
speedMultiplicator = 1;
horizontalDivisor = 1;
}
realInputAxis = Vector3(Input.GetAxis("Horizontal") / horizontalDivisor, 0, Input.GetAxis("Vertical"));
realSpeed = speed * speedMultiplicator;
var transformedRealInputAxis = transform.TransformDirection(realInputAxis);
playerRigidbody.MovePosition(transform.position + transformedRealInputAxis * realSpeed * Time.deltaTime);
if(Input.GetButtonDown("Jump") && grounded){
playerRigidbody.AddForce(Vector3.up * jumpForce * 25, ForceMode.Impulse);
}
var hitColliders = Physics.OverlapSphere(check + transform.position, groundSphereRadius, groundLayers);
if(hitColliders.Length == 0){
grounded = false;
}else{
grounded = true;
}
//Fake friction
var easeVelocityFactor : float;
easeVelocityFactor *= 0.75;
playerRigidbody.velocity.x = easeVelocityFactor;
playerRigidbody.velocity.z = easeVelocityFactor;
}
function Update () {
}
function OnDrawGizmosSelected() {
if(debugCollision){
if(grounded){
Gizmos.color = Color.green;
}else{
Gizmos.color = Color.red;
}
Gizmos.DrawSphere(check + transform.position, groundSphereRadius);
}
}