Question by
Big2 · Oct 11, 2015 at 10:54 AM ·
raycastingmovement script
Landing doesnt work
hi guys, i got a problem about my movement. i want to get the falling speed of the player, and a raycast telling me that the player is grounded. here is my script:
public float speed = 1F;
public float sprintSpeed = 3F;
public float jumpSpeed = 7F;
public float gravity = 20F;
public float v;
float RunCycleLegOffset = 0.2F;
const float k_Half = 0.5f;
float fallingspeed2;
public Vector3 moveDirection = Vector3.zero;
CharacterController cc;
public Animator anim;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController> ();
anim = GetComponent<Animator> ();
}
void FixedUpdate () {
float h = Input.GetAxis ("Horizontal");
anim.SetFloat("Direction", h);
v = Input.GetAxis("Vertical");
}
// Update is called once per frame
void Update () {
float runCycle = Mathf.Repeat(anim.GetCurrentAnimatorStateInfo(0).normalizedTime + RunCycleLegOffset, 1);
float jumpLeg = (runCycle < k_Half ? -1 : 1) * Input.GetAxis ("Vertical");
//Debug.Log (jumpLeg);
//======================================================================================================
if (cc.isGrounded)
{
moveDirection = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
moveDirection = transform.TransformDirection (moveDirection);
moveDirection *= speed;
anim.SetFloat("JumpLeg", jumpLeg);
if (Input.GetButtonDown("Jump") && Input.GetAxis ("Vertical") >= 0)
{
moveDirection.y = jumpSpeed;
anim.SetBool("Space", true);
}
else
anim.SetBool("Space", false);
if (Input.GetAxis ("Vertical") > 0.01 && Input.GetKey(KeyCode.LeftShift))
{
sprint();
v += 1;
}
}
moveDirection.y -= gravity * Time.deltaTime;
cc.Move (moveDirection * Time.deltaTime);
//=======================================================================================================
if(!cc.isGrounded)
{
if(moveDirection.y < fallingspeed2)
{
fallingspeed2 = moveDirection.y;
}
}
Debug.Log (fallingspeed2);
float landing = (fallingspeed2 < -10 ? 1 : 0);
//=======================================================================================================
RaycastHit hit;
float downraylength = cc.height / 2 + 0.02f;
if(Physics.Raycast(transform.position, Vector3.down, out hit, downraylength))
{
anim.SetBool("isGrounded", true);
Debug.Log ("grounded");
fallingspeed2 = 0;
}
anim.SetBool("isGrounded", false);
Debug.DrawRay (transform.position, transform.TransformDirection(Vector3.down) * downraylength, Color.green);
//=======================================================================================================
anim.SetFloat ("Landing", landing);
anim.SetFloat ("Speed", v);
float y = moveDirection.y;
anim.SetFloat ("verticalSpeed", y);
float fall = fallingspeed2;
anim.SetFloat ("fallingSpeed", fall);
Debug.Log (moveDirection.y);
//========================================================================================================
if(anim.GetCurrentAnimatorStateInfo(0).IsName("roll"))
{
moveDirection *= 0;
fallingspeed2 = 0;
}
}
void sprint () {
moveDirection.z *= sprintSpeed;
moveDirection.x *= sprintSpeed;
}
its my whole movement script. i know its a bit messy
the speed parameter works, but the isgrounded mechanism doesnt seem to work. the player is grounded but when its in the animator, it doesnt work, and the player will forever stay in the falling animation even when its on the ground. i dont even know why so please help
Comment