当它朝某个方向移动时翻转游戏对象
Flip gameObject when it goes a certain direction
我正在制作一个玩家只能上下左右移动的 2.5D 游戏;无 X 轴
我有这样的控件设置:
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed = 8;
private Vector3 scale = new Vector3(5, 5, 5);
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
transform.localScale = scale;
if (Input.GetKey(KeyCode.S))
{
transform.localScale = new Vector3(5, 2, 5);
}
}
void FixedUpdate()
{
float mH = -Input.GetAxis("Horizontal");
rb.velocity = new Vector3(0, rb.velocity.y, mH * speed);
}
}
问题是角色在朝相反的方向前进时应该转弯,
我可以通过旋转来翻转它或将 Z 比例设置为负值,但这会使它立即转动,我想让它在几帧的跨度内转动,这样看起来就不会不自然。我怎样才能继续实施这样的事情?甚至可以统一吗?
例子 i have
vs 我会 like to have:
Is it even possible in unity?
大多数事情都是可能的..问题总是需要多少努力;)
可能有很多方法可以实现。
我会简单地向所需的前进方向平滑插值。比如
public class Movement : MonoBehaviour
{
public float speed = 8;
private Vector3 normalScale = new Vector3(5, 5, 5);
private Vector3 duckedScale = new Vector3(5, 2, 5);
private Rigidbody rb;
public float rotationSpeed = 5f;
private Vector3 targetForwardDirection;
private Vector3 currentForwardDirection;
void Start()
{
rb = GetComponent<Rigidbody>();
targetForwardDirection = transform.forward;
currentForwardDirection = transform.forward;
}
void Update()
{
transform.localScale = Input.GetKey(KeyCode.S) ? duckedScale : normalScale;
}
void FixedUpdate()
{
var mH = -Input.GetAxis("Horizontal");
rb.velocity = new Vector3(0, rb.velocity.y, mH * speed);
if (!Mathf.Approximately(mH, 0))
{
targetForwardDirection = new Vector3(0, 0, mH * speed).normalized;
}
currentForwardDirection = Vector3.Slerp(currentForwardDirection, targetForwardDirection, rotationSpeed * Time.deltaTime);
rb.MoveRotation(Quaternion.LookRotation(currentForwardDirection));
}
}
我正在制作一个玩家只能上下左右移动的 2.5D 游戏;无 X 轴
我有这样的控件设置:
using UnityEngine;
public class Movement : MonoBehaviour
{
public float speed = 8;
private Vector3 scale = new Vector3(5, 5, 5);
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
transform.localScale = scale;
if (Input.GetKey(KeyCode.S))
{
transform.localScale = new Vector3(5, 2, 5);
}
}
void FixedUpdate()
{
float mH = -Input.GetAxis("Horizontal");
rb.velocity = new Vector3(0, rb.velocity.y, mH * speed);
}
}
问题是角色在朝相反的方向前进时应该转弯, 我可以通过旋转来翻转它或将 Z 比例设置为负值,但这会使它立即转动,我想让它在几帧的跨度内转动,这样看起来就不会不自然。我怎样才能继续实施这样的事情?甚至可以统一吗?
例子 i have vs 我会 like to have:
Is it even possible in unity?
大多数事情都是可能的..问题总是需要多少努力;)
可能有很多方法可以实现。
我会简单地向所需的前进方向平滑插值。比如
public class Movement : MonoBehaviour
{
public float speed = 8;
private Vector3 normalScale = new Vector3(5, 5, 5);
private Vector3 duckedScale = new Vector3(5, 2, 5);
private Rigidbody rb;
public float rotationSpeed = 5f;
private Vector3 targetForwardDirection;
private Vector3 currentForwardDirection;
void Start()
{
rb = GetComponent<Rigidbody>();
targetForwardDirection = transform.forward;
currentForwardDirection = transform.forward;
}
void Update()
{
transform.localScale = Input.GetKey(KeyCode.S) ? duckedScale : normalScale;
}
void FixedUpdate()
{
var mH = -Input.GetAxis("Horizontal");
rb.velocity = new Vector3(0, rb.velocity.y, mH * speed);
if (!Mathf.Approximately(mH, 0))
{
targetForwardDirection = new Vector3(0, 0, mH * speed).normalized;
}
currentForwardDirection = Vector3.Slerp(currentForwardDirection, targetForwardDirection, rotationSpeed * Time.deltaTime);
rb.MoveRotation(Quaternion.LookRotation(currentForwardDirection));
}
}