似乎什么都不起作用,我尝试了几个小时,从谷歌上复制和粘贴“解决方案”,但都没有效果。
更改maxSpeed变量并没有任何作用。对象仍然像巴里·艾伦一样飞过屏幕。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[Range (0, 5)]public int speed;
public Rigidbody2D rb2D;
public Vector3 veloc;
public float maxSpeed;
void Start ()
{
rb2D = GetComponent<Rigidbody2D> ();
speed = 4;
maxSpeed = 0.01f;
veloc = GetComponent <Rigidbody2D>().velocity;
}
void Update ()
{
if(Input.GetKey(KeyCode.W))
{
rb2D.AddForce (Vector3.up * speed);
}
if(Input.GetKey(KeyCode.S))
{
rb2D.AddForce (-Vector3.up * speed);
}
if(Input.GetKey(KeyCode.D))
{
rb2D.AddForce (Vector3.right * speed);
}
if(Input.GetKey(KeyCode.A))
{
rb2D.AddForce (-Vector3.right * speed);
}
}
void FixedUpdate ()
{
if(veloc.magnitude > maxSpeed)
{
rb2D.velocity = rb2D.velocity.normalized * maxSpeed;
}
}
}
FixedUpdate
的if语句?rb2D.velocity.normalized
的值是多少?maxSpeed
的值是多少? - Camilo Terevintoveloc.magnitude
的值只在 Start 方法中读取,因此它始终为 0。相反,我将其更改为rb2D.velocity.magnitude
,以便随着对象在屏幕上移动而不断更新。 - Adi