按下W键时如何让角色行走,停止按下时如何让角色行走idle/grounded?
How can i make character walking when pressing W key and idle/grounded when stop pressing?
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;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
if (Input.GetKeyDown("w"))
{
_anim.Play("Walk");
}
else
{
_anim.Play("Grounded");
}
}
}
我刚做的时候:
_anim.Play("Walk");
角色不停地向前走。
但现在我想让他在按住 W 时走路,不按 W 时 idle/grounded.
但是角色没有走路就在移动。
脚本附加到第三人称角色。
即使未按下 'w',您也在翻译转换。将翻译移到 if 案例中,一切都会按计划进行。另外 GetKeyDown
应该替换为 GetKey
并且当前状态应该被保存,因为第一个的定义是:
You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again. - Input.GetKeyDown
代码可能如下所示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CC : MonoBehaviour
{
private Vector3 moveDirection = Vector3.zero;
private Animator _anim;
private bool _isWalking = false;
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"))
{
if(!_isWalking)
{
_isWalking = true;
_anim.Play("Walk");
}
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Translate(0, 0, z); // Only move when "w" is pressed.
}
else
{
if(_isWalking)
{
_anim.Play("Grounded");
}
_isWalking = false;
}
}
}
update()
在每个新帧上调用,但 getKeyDown()
方法仅在用户按下按钮的帧期间 returns 为真。由于您一直按 w 键,语句返回 false 并执行 "grounded" 块。
尝试在 Input.GetKeyDown()
开始播放动画,但仅在 Input.GetKeyUp();
时将动画更改为空闲
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;
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Rotate(0, x, 0);
transform.Translate(0, 0, z);
if (Input.GetKeyDown("w"))
{
_anim.Play("Walk");
}
else
{
_anim.Play("Grounded");
}
}
}
我刚做的时候:
_anim.Play("Walk");
角色不停地向前走。 但现在我想让他在按住 W 时走路,不按 W 时 idle/grounded.
但是角色没有走路就在移动。
脚本附加到第三人称角色。
即使未按下 'w',您也在翻译转换。将翻译移到 if 案例中,一切都会按计划进行。另外 GetKeyDown
应该替换为 GetKey
并且当前状态应该被保存,因为第一个的定义是:
You need to call this function from the Update function, since the state gets reset each frame. It will not return true until the user has released the key and pressed it again. - Input.GetKeyDown
代码可能如下所示:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CC : MonoBehaviour
{
private Vector3 moveDirection = Vector3.zero;
private Animator _anim;
private bool _isWalking = false;
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"))
{
if(!_isWalking)
{
_isWalking = true;
_anim.Play("Walk");
}
var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;
transform.Translate(0, 0, z); // Only move when "w" is pressed.
}
else
{
if(_isWalking)
{
_anim.Play("Grounded");
}
_isWalking = false;
}
}
}
update()
在每个新帧上调用,但 getKeyDown()
方法仅在用户按下按钮的帧期间 returns 为真。由于您一直按 w 键,语句返回 false 并执行 "grounded" 块。
尝试在 Input.GetKeyDown()
开始播放动画,但仅在 Input.GetKeyUp();