Visual studio 运动系统和短跑不一起工作-统一
我一直在努力学习使用unity,对我来说最困难的部分是学习编程。我正在尝试创建一个简单的2d自上而下的游戏。我的角色有一个坚实的运动系统,还有一个不那么坚实的短跑。但问题是,这两件事不想一起工作。我的角色可以在完整代码处于活动状态的情况下四处走动,但不能使用破折号。如果我去掉了保证占有权变更的那条线,破折号就会神奇地开始工作 我将把我的代码粘贴到这里:Visual studio 运动系统和短跑不一起工作-统一,visual-studio,unity3d,if-statement,Visual Studio,Unity3d,If Statement,我一直在努力学习使用unity,对我来说最困难的部分是学习编程。我正在尝试创建一个简单的2d自上而下的游戏。我的角色有一个坚实的运动系统,还有一个不那么坚实的短跑。但问题是,这两件事不想一起工作。我的角色可以在完整代码处于活动状态的情况下四处走动,但不能使用破折号。如果我去掉了保证占有权变更的那条线,破折号就会神奇地开始工作 我将把我的代码粘贴到这里: public class Player_movement : MonoBehaviour { public float moveSpe
public class Player_movement : MonoBehaviour
{
public float moveSpeed;
public Rigidbody2D rb;
public float DashSpeed;
private float DashTime;
public float startDashTime;
private int direction;
Vector2 movement;
void Start()
{rb = GetComponent<Rigidbody2D>();
DashTime = startDashTime;
}
void Update()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if(direction == 0)
{
if(Input.GetKeyDown(KeyCode.LeftArrow)){
direction = 1;
}else if(Input.GetKeyDown(KeyCode.RightArrow)){
direction = 2;
}else if(Input.GetKeyDown(KeyCode.UpArrow)){
direction = 3;
}else if(Input.GetKeyDown(KeyCode.DownArrow)){
direction = 4;
}
}
else
{
if(DashTime <= 0){
direction = 0;
DashTime = startDashTime;
rb.velocity = Vector2.zero;}
else
{
DashTime -= Time.deltaTime;
{
if(direction == 1){
rb.velocity = Vector2.left * DashSpeed;
}else if(direction == 2){
rb.velocity = Vector2.right * DashSpeed;
}else if(direction == 3){
rb.velocity = Vector2.up * DashSpeed;
}else if(direction == 4){
rb.velocity = Vector2.down * DashSpeed;
}
}
}
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
}
public class Player_运动:单一行为
{
公共交通速度;
公共刚体2d rb;
公共交通速度;
私人时间;
公众浮标启动时间;
私人int方向;
矢量2运动;
void Start()
{rb=GetComponent();
DashTime=开始DashTime;
}
无效更新()
{
movement.x=Input.GetAxisRaw(“水平”);
movement.y=Input.GetAxisRaw(“垂直”);
如果(方向==0)
{
if(Input.GetKeyDown(KeyCode.LeftArrow)){
方向=1;
}else if(Input.GetKeyDown(KeyCode.RightArrow)){
方向=2;
}else if(Input.GetKeyDown(KeyCode.UpArrow)){
方向=3;
}else if(Input.GetKeyDown(KeyCode.DownArrow)){
方向=4;
}
}
其他的
{
如果(DashTime调用刚体上的MovePosition类似于一个覆盖,它迫使将刚体插值到您给定的位置。因此,刚体不会响应其他运动,例如使用速度的破折号代码
解决这个问题的一个简单方法是在你像这样冲刺时设置一个bool
isDashing = true;
然后,在你的固定更新中,你可以做你的移动位置,只有当你没有冲刺,如
if (!isDashing) {
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
这样,您可以在冲刺时使用基于速度的移动,而在不冲刺时使用基于移动位置的移动
只要记住在破折号结束时将isDashing变量设置为false,这样控制就会传回运动代码