![](/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.