我在用unity做一个游戏,想问一下,在PC上按键时,如何让玩家的y_axis向下缩放,而松开键时,y_axis向上缩放?
使用System; 使用UnityEngine;
public class Playermovement : MonoBehaviour {
public Rigidbody rb;
public float forwardforce = 2000f;
public float sideswaysforce = 500f;
Vector3 small;
// Update is called once per frame
void FixedUpdate()
{
rb.AddForce(0, 0, forwardforce * Time.deltaTime);
if(Input.GetKey("d"))
{
rb.AddForce(sideswaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sideswaysforce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if(Input.GetKey("f"))
{
small = transform.localScale;
small.y = 0.5f;
transform.localScale = small;
}
}
}
}
你可以这样做,在你现在的代码中,你可以不断缩小比例尺,什么都不重置。
public Vector3 halfScale;
public Vector3 originalScale;
void Start() {
originalScale = transform.localScale;
halfScale = originalScale;
halfScale.y = halfScale.y * 0.5f;
}
//...in your fixedupdate loop
if(Input.GetKey("f"))
{
transform.localScale = halfScale;
} else {
transform.localScale = originalScale;
}