我的角色正在制作动画,但仍然无法移动

我刚刚开始团结,也正在为学校项目制作一款游戏。当我按下“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);

        }

    }




}


蓝山帝景
浏览 94回答 1
1回答

HUH函数

您将“temp”设置为等于 transform.position,但这并不意味着 transform.position 等于 'temp'。在这里,下面的脚本应该给你你想要的using UnityEngine;public class PlayerController : MonoBehaviour{private float speed = 3f;private Animator anim;private SpriteRenderer sr;void Awake(){&nbsp; &nbsp; anim = GetComponent<Animator>();&nbsp; &nbsp; sr = GetComponent<SpriteRenderer>();}void Update(){&nbsp; &nbsp; Move();}void Move(){&nbsp; &nbsp; float h = Input.GetAxisRaw("Horizontal");&nbsp; &nbsp; transform.Translate(Vector2.right * (h * speed * Time.deltaTime));&nbsp; &nbsp; anim.SetBool("Walk", h != 0f);&nbsp; &nbsp; if (anim.GetBool("Walk"))&nbsp; &nbsp; &nbsp; &nbsp; Flip(h > 0f);}void Flip(bool facingRight){&nbsp; &nbsp; sr.flipX = !facingRight;}}
打开App,查看更多内容
随时随地看视频慕课网APP