- Home /
Question by
haimmoshe · Aug 31, 2017 at 01:32 PM ·
c#scripting problemscript.
How can i switch smooth between humanoidwalk and humanoididle states ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CC : MonoBehaviour
{
private Vector3 moveDirection = Vector3.zero;
private Animator _anim;
private void Start()
{
_anim = GetComponent<Animator>();
}
void Update()
{
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
transform.Rotate(0, x, 0);
if (Input.GetKey("w"))
{
_anim.Play("Walk");
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Translate(0, 0, z); // Only move when "w" is pressed.
}
else
{
_anim.Play("Idle");
}
}
}
The problem is when leave the key W it's just at once switch to idle state. But how can i make somehow that it will slowly change from walk to idle ?
Comment
Best Answer
Answer by cgarossi · Aug 31, 2017 at 02:56 PM
Look into blendtrees. This will allow you to blend the animation according to a value.
I made tutorial on this:
Your answer
![](https://koobas.hobune.stream/wayback/20220612133931im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I animate linerenderer lines over time ? 1 Answer
How can i find a thirdpersoncontroller hand and attach object to the hand ? 1 Answer
How can i check/wait until the gameobject will end the rotation ? 1 Answer
How can I call the Load method and/or the ShootingSettings method also only once in the Update ? 1 Answer
How can i check and fire an event when the user look at specific object ? 0 Answers