![](/img/trans.png)
[英]My Unity 2D player controller script won't make my character flip when he moves to a different direction
[英]How to make my 2D character 'look' or flip in the direction he is moving?
我是 Unity 的新手,我正在学习/制作我的第一款游戏,这将是一款平台游戏。 我想先让运动完美。 我添加了一个简单的脚本,让玩家可以移动和跳跃。
这里是:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour{
public float jumpspeed = 5f;
public float speed = 5f;
Rigidbody2D rb;
GameObject character;
// Start is called before the first frame update
void Start()
{
}
void Awake(){
rb = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)){
Jump();
}
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += move *Time.deltaTime * speed;
}
void Jump(){
rb.AddForce(new Vector2(0f, jumpspeed), ForceMode2D.Impulse);
}
}
现在,我希望角色面向它移动的方向。 默认情况下,png 朝向右侧。
我怎样才能做到这一点? 另外,我可以让我的动作脚本更好吗?
提前致谢!
我通常使用基于 bool 的代码来执行此操作。
void FlipSprite()
{
isFacingRight = !isFacingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.