player.SetInteger("Status",1);
的狀態給予要動作的按鍵。接著後面要加入Bool函數和條件式(if,else)來避免重複賦予狀態的情況發生,最後才能成功切換。using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moving : MonoBehaviour
{
public Rigidbody2D rigid2D;
public Animator player;
void Start()
{
}
void Update()
{
bool walk = false;
if (Input.GetKeyDown("space"))
{
rigid2D.AddForce(new Vector2(0, 26),ForceMode2D.Impulse);
print("jump");
}
if (Input.GetKey("left"))
{
player.SetInteger("Status",1);
walk = true;
rigid2D.AddForce(new Vector2(-3, 0), ForceMode2D.Force);
transform.localScale = new Vector3(-10, 10, 10);
print("moveleft");
}
if (Input.GetKey("right"))
{
player.SetInteger("Status", 1);
walk = true;
rigid2D.AddForce(new Vector2(3, 0), ForceMode2D.Force);
print("moveright");
transform.localScale = new Vector3(10, 10, 10);
}
if (walk)
{
if (player.GetInteger("Status") == 0)
player.SetInteger("Status", 1);
}
else
{
if (player.GetInteger("Status") == 1)
player.SetInteger("Status", 0);
}
}
}
1.https://www.youtube.com/watch?v=aIOYDtJnJI0&t=692s&ab_channel=%E9%98%BF%E7%A9%BA%E7%9A%84%E9%81%8A%E6%88%B2%E9%83%A8%E5%B1%8B
2.https://ithelp.ithome.com.tw/articles/10235140?sc=iThelpR