- Home /
RigidBody Character Controller Bug
I have been working on this character controller for a while, but there is a bug that has stumped me for several weeks now. What I am trying to achieve is when the character falls, say off of a ledge, it will fall over and when it hits the ground roll around a bit slow down and finally get up. What happens at the moment is that the character can fall of a ledge, act like a rigidbody, but when it hits the ground it just snaps back up instantly. I don't have any idea what is going on here. Code:
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
public float acceleration = 10;
public float maxSpeed = 5;
public float jump = 150;
public bool grounded;
public Transform camera;
public float lerp = 1;
public float fallSpeed = 10;
public float health = 100;
public float fallDamage = 0.1f;
public bool fall;
public float damage;
float mouseSensitivity;
float horizontalRot = 0.0f;
float currentYRot;
float yRotV;
public float maxSlope = 70.0f;
Vector2 horizontalMove;
void Start ()
{
mouseSensitivity = camera.GetComponent<CameraControl>().mouseSensitivity;
}
void Update ()
{
if(Physics.Raycast(transform.position, new Vector3(0, -1, 0), 1))
{
Grounded();
fall = false;
Move();
}
else
{
grounded = false;
Fall();
}
//if(rigidbody.velocity.magnitude >= maxSpeed + 3)
//{
// Fall();
//}
//horizontalRot += Input.GetAxis("Mouse X") * mouseSensitivity;
//currentYRot = Mathf.SmoothDamp(currentYRot, horizontalRot, ref yRotV, smoothDamp);
if(!fall)
{
transform.rotation = Quaternion.Euler(transform.rotation.x, camera.transform.rotation.eulerAngles.y, transform.rotation.z);
}
horizontalMove = new Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if(horizontalMove.magnitude > maxSpeed)
{
horizontalMove = horizontalMove.normalized;
horizontalMove *= maxSpeed;
}
rigidbody.velocity = new Vector3(horizontalMove.x, rigidbody.velocity.y, horizontalMove.y);
}
void Fall()
{
rigidbody.constraints = RigidbodyConstraints.None;
grounded = false;
fall = true;
//while(rigidbody.velocity.magnitude <= 0.2f)
//{
// gameObject.transform.eulerAngles = new Vector3(0, gameObject.transform.eulerAngles.y, 0);
// fall = false;
//}
}
void Grounded()
{
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
grounded = true;
}
void Move()
{
if(grounded && !fall)
{
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * acceleration, 0, Input.GetAxis("Vertical") * acceleration);
if(Input.GetKeyDown(KeyCode.Space))
{
transform.rigidbody.AddForce(Vector3.up * jump);
}
}
}
}
Any answer that leads in the right direction is appreciated, thanks!
In your condition "if(!fall)" you correct not work with Quaternion. Quaternion is four component vector. Each its value has the range from 0 to 1. But function Quatenion.Euler(x, y, z) converts rotation angles of Euler in degrees to a quaternion. In most cases, the value range x, y and z will be from 0 to 360.
The problem with that method is that the player won't rotate at all any more. Physics will act on it, but when the player starts rotating it gets rotation set back to 0. It just ends up as a bunch of jittering.
Certainly, player won't rotate. You as Grounded() forbid rotations through rigidbody.
Even if I don't freeze the rotation, the player won't rotate.