- Home /
Gravity makes player slide on any slope!!! Check if rigidbody Isgrounded not working
I have two scripts, they make the character move horizontally and vertically, and I have added gravity but the character keeps sliding on solpes, I want to stop that so the character never slides. By the way the void is grounded doesnt help very well. Also can you help me understand how to make the void IsGrounded work, so unity detects if the rigidbosy is Grounded? You can test this script in a player. Remember that you have to add a rigidbody, and a collider, and also disable rigidbody's rotations.
Here are my two scripts:
1st script TP_Motor
using UnityEngine;
using System.Collections;
public class TP_Motor : MonoBehaviour {
public static TP_Motor Instance;
public float MoveSpeed = 40f;
public float Gravity = 10f;
public float TerminalVelocity = 20f;
public Vector3 MoveVector { get; set; }
public float VerticalVelocity { get; set; }
void Awake() // Debe quedarse en awake
{
Instance = this;
}
public void UpdateMotor() // Debe quedarse en update motor
{
ProcessMotion();
}
void ProcessMotion()
{
// Transform MoveVector to World Space
MoveVector = transform.TransformDirection(MoveVector);
// Normalize MoveVector if Magnitude > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// Multiply MoveVector by MoveSpeed
MoveVector *= MoveSpeed;
// Reapply VerticalVelocity MoveVector.y
MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);
// Apply Gravity
ApplyGravity();
// Move Character in World Space
TP_Controller.Rigidbody2.velocity = MoveVector;
}
void ApplyGravity()
{
if (MoveVector.y > -TerminalVelocity)
MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
if ((TP_Controller.Instance.isgrounded == false) && MoveVector.y < -1)
MoveVector = new Vector3(MoveVector.x, -1, MoveVector.z);
}
}
2nd script TP_Controller
using UnityEngine; using System.Collections;
public class TP_Controller : MonoBehaviour { public static Rigidbody Rigidbody2; public static TP_Controller Instance;
public bool isgrounded;
private float distToGround;
void Awake () // Debe quedarse en awake
{
Rigidbody2 = GetComponent("Rigidbody") as Rigidbody;
Instance = this;
distToGround = collider.bounds.extents.y;
}
void Update ()
{
if (Camera.mainCamera == null)
return;
GetLocomotionInput();
TP_Motor.Instance.UpdateMotor();
}
void IsGrounded()
{
if (Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f))
isgrounded = true; // Debe mantenerse en true
}
void GetLocomotionInput()
{
var deadZone = 0.1f;
TP_Motor.Instance.VerticalVelocity = TP_Motor.Instance.MoveVector.y;
TP_Motor.Instance.MoveVector = Vector3.zero;
if (Input.GetAxis("Vertical") > deadZone || Input.GetAxis("Vertical") < -deadZone)
TP_Motor.Instance.MoveVector += new Vector3(0, 0, Input.GetAxis("Vertical"));
if (Input.GetAxis("Horizontal") > deadZone || Input.GetAxis("Horizontal") < -deadZone)
TP_Motor.Instance.MoveVector += new Vector3(Input.GetAxis("Horizontal"), 0, 0);
}
}