Adi*_*Adi 3 c# unity-game-engine
似乎没有任何效果,玩了几个小时,从谷歌复制并粘贴了“解决方案”,但没有。
更改 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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
只有这个:
void Update() {
rb2D.velocity = Vector3.ClampMagnitude(rb2D.velocity, maxSpeed);
}
Run Code Online (Sandbox Code Playgroud)