using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public GameObject player;
public Rigidbody2D playerRB;
public Transform playerTF;
public float moveForce;
public float rotateForce;
private string currentMoveKey = "";
private string currentRotateKey = "";
void Update()
{
//move
if(Input.GetKey("w") && currentMoveKey == "")
{
currentMoveKey = "w";
playerRB.AddForce(transform.up * moveForce * Time.deltaTime);
}
if (Input.GetKeyUp("w") && currentMoveKey == "w")
{
playerRB.velocity = new Vector2(0, 0);//resets when keyup and not already reset
currentMoveKey = "";
}
if (Input.GetKey("s") && currentMoveKey == "")
{
currentMoveKey = "s";
playerRB.AddForce(transform.up * -moveForce * Time.deltaTime);
}
if (Input.GetKeyUp("s") && currentMoveKey == "s")
{
playerRB.velocity = new Vector2(0, 0);
currentMoveKey = "";
}
//rotate
if (Input.GetKeyDown("a") && currentRotateKey == "")
{
currentRotateKey = "a";
}
if (Input.GetKey("a") && currentRotateKey == "a")
{
playerTF.Rotate(transform.forward * rotateForce * Time.deltaTime);
}
if (Input.GetKeyUp("a") && currentRotateKey == "a")
{
currentRotateKey = "";
}
if (Input.GetKeyDown("d") && currentRotateKey == "")
{
currentRotateKey = "d";
}
if (Input.GetKey("d") && currentRotateKey == "d")
{
playerTF.Rotate(transform.forward * -rotateForce * Time.deltaTime);
}
if (Input.GetKeyUp("d") && currentRotateKey == "d")
{
currentRotateKey = "";
}
}
}
嘿!由于某些原因,在我将力添加到RigidBody2D的那一行中,如果我在移动球员的同时旋转它,从而改变了指向的位置,那么球员将继续朝着他最初指向的方向移动。换句话说,他看起来像他在滑动,我必须停止移动并重新开始移动以更新他指向的方向。我该怎么做才能解决此问题,以便在他旋转时,他的移动方向会同时更新?
加力只是不断增加刚体的动量。自从您在举起琴键时重置速度后,就已经知道了这一点。当您转身但保持向前移动时,刚体速度不会被重置,而只是进行了相加更改。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public GameObject player;
public Rigidbody2D playerRB;
public Transform playerTF;
public float moveForce;
public float rotateForce;
private Vector3 movement;
private string currentRotateKey = "";
void Update()
{
//move in Local space
if (Input.GetKeyDown(KeyCode.W))
{
movement += Vector3.up;
}
if (Input.GetKeyUp(KeyCode.W))
{
movement -= Vector3.up;
}
if (Input.GetKeyDown(KeyCode.S))
{
movement -= Vector3.up;
}
if (Input.GetKeyUp(KeyCode.S))
{
movement += Vector3.up;
}
//rotate
if (Input.GetKeyDown("a") && currentRotateKey == "")
{
currentRotateKey = "a";
}
if (Input.GetKey("a") && currentRotateKey == "a")
{
playerTF.Rotate(transform.forward * rotateForce * Time.deltaTime);
}
if (Input.GetKeyUp("a") && currentRotateKey == "a")
{
currentRotateKey = "";
}
if (Input.GetKeyDown("d") && currentRotateKey == "")
{
currentRotateKey = "d";
}
if (Input.GetKey("d") && currentRotateKey == "d")
{
playerTF.Rotate(transform.forward * -rotateForce * Time.deltaTime);
}
if (Input.GetKeyUp("d") && currentRotateKey == "d")
{
currentRotateKey = "";
}
// Implicit cast from Vector3 to Vector2, takes X and Ys
ApplyMovement(movement);
}
public void ApplyMovement(Vector2 movement)
{
playerRB.velocity = player.transform.TransformDirection(movement.x, movement.y, 0) * moveForce * Time.deltaTime;
}
}
我发现使用KeyCode
枚举而不是字符串值会更干净一些,而且我喜欢Vector3 movement
为该类跟踪局部变量而不是使用check currentMovementKey
。这样,您可以一次按下多个键,并在编译时捕获拼写错误。
将您的动作“应用”到自己的方法中有助于在需要时更改行为。
movement
在局部空间中处理,使用Vector3.up
而不是transform.up
避免旋转时出错,然后将其应用于时,playerRB.velocity
我们可以使用转换回世界空间player.transform.TransformDirection(movement.x, movement.y, 0)
(也可以transform.TransformDirection(movement.x, movement.y, 0)
是该组件与RigidBody2D组件位于同一GameObject上)。
如果您想保持加速度而不是想跳起来而不是掉下来,但是又不想像真实物体那样漂移,事情会变得有些棘手,这是解决问题的一种方法:
public void ApplyMovement(Vector3 movement)
{
if (movement.sqrMagnitude > Mathf.Epsilon)
{
playerRB.drag = 0; // or some other small value
if (playerRB.velocity.sqrMagnitude > Mathf.Epsilon)
{
playerRB.velocity = player.transform.TransformDirection(movement.normalized) * playerRB.velocity.magnitude;
}
playerRB.AddRelativeForce(movement * moveForce * Time.deltaTime);
}
else
{
playerRB.drag = 100f; // or some other value larger than the small one
}
}
现在,我们正在检查是否要应用不可忽略的运动量(基本上为0),如果要进行运动,则不施加阻力,因为那样会减慢正加速度这样一来,很难预测设置游戏时应具有的值。
由于我们现在没有在物体上的阻力,因此它应该根据物理原理漂移。这就是您目前所看到的。如果我们不希望这样做,可以检查是否有不可忽略的速度(基本上为0),并将其与所需的运动方向对齐。不要忘记那movement
仍然在局部空间,而playerRB.velocity
在世界空间。
然后,由于我们有要应用的运动,因此应该将我们的局部运动应用到我们的身体,AddRelativeVelocity
以继续沿所需方向加速。目前,这种情况一直持续下去,但是您最终可能希望限制最大速度。
如果我们不应用运动,那么我们希望物理学应用阻力并降低玩家的速度。如果我们让物理学做功,您应该获得合理的加速或减速速度(取决于阻力值和作用力),但是由于我们忽略了漂移,因此您应该立即转向。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句