我刚刚开始团结,也正在为学校项目制作一款游戏。当我按下“a”和“d”键并且子画面翻转时,我的2d游戏角色可以执行步行动画,但它保持在相同的位置。以下是我的玩家控制器脚本using System.Collections;using System.Collections.Generic;using UnityEngine;public class PlayerController : MonoBehaviour{ private float speed = 3f; private Animator anim; private SpriteRenderer sr; // Start is called before the first frame update void Awake() { anim = GetComponent<Animator>(); sr = GetComponent<SpriteRenderer>(); } // Update is called once per frame void Update() { Move(); } void Move() { float h = Input.GetAxisRaw("Horizontal"); Vector3 temp = transform.position; if (h > 0) { temp.x += speed * Time.deltaTime; sr.flipX = true; anim.SetBool("Walk", true); } else if (h < 0) { temp.x -= speed * Time.deltaTime; sr.flipX = false; anim.SetBool("Walk", true); } else if (h == 0) { anim.SetBool("Walk", false); } }}
1 回答
HUH函数
TA贡献1836条经验 获得超4个赞
您将“temp”设置为等于 transform.position,但这并不意味着 transform.position 等于 'temp'。在这里,下面的脚本应该给你你想要的
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private float speed = 3f;
private Animator anim;
private SpriteRenderer sr;
void Awake()
{
anim = GetComponent<Animator>();
sr = GetComponent<SpriteRenderer>();
}
void Update()
{
Move();
}
void Move()
{
float h = Input.GetAxisRaw("Horizontal");
transform.Translate(Vector2.right * (h * speed * Time.deltaTime));
anim.SetBool("Walk", h != 0f);
if (anim.GetBool("Walk"))
Flip(h > 0f);
}
void Flip(bool facingRight)
{
sr.flipX = !facingRight;
}
}
- 1 回答
- 0 关注
- 85 浏览
添加回答
举报
0/150
提交
取消