- Home /
if !grounded
Hey! Im using this script for my character movement using UnityEngine; using System.Collections;
public class Controller : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
private Vector3 moveDirection = Vector3.zero;
public GameObject target;
private Transform myTransform;
//Animations
//public AnimationClip Run;
public AnimationClip Takeoff;
public AnimationClip Jump;
public AnimationClip Idle;
public float jumpAnimationSpeed;
public float walkAnimationSpeed;
void Start(){
myTransform = transform;
animation["Jump"].layer=1;
}
void Update() {
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded) {
moveDirection = new Vector3( 0, 0, Input.GetAxis("Horizontal"));
//moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetAxis("Horizontal") > .02f)
{
animation.CrossFade("Run");
}
else if (Input.GetAxis("Horizontal") < -.02f)
{
animation.Play("Run");
}
else
{
animation.Play ("Idle");
}
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
animation.Play("Jump");
}
}
if (Input.GetMouseButton(1))
{
Debug.Log (" Im clicking fire2");
moveDirection = new Vector3( 0, 5, 0);
if (controller.isGrounded)
animation.Play("Takeoff");
}
if (!controller.isGrounded)
animation.Play ("JumpStand");
if(target) {
myTransform.LookAt(target.transform);
myTransform.localEulerAngles = new Vector3(0,myTransform.localEulerAngles.y, 0);
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
and what Im having problem with is this part if (!controller.isGrounded) animation.Play ("JumpStand");
Because the character sometimes can accidently get a tiny bit up in the air when Im going downstairs and that activates the JumpStand animation and brakes the walk animation, I was wondering if I somehow can activate " if (!conroller.isGrounded) " if Im above the ground by a tiny bit? and not instantly. Like " if the character is above the ground by 0.5 or more on the Y axis animatin.Play ("JumpStand")
Your answer
![](https://koobas.hobune.stream/wayback/20220613133431im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
FBX Animation imports wrongly 1 Answer
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers