简体   繁体   English

将玩家旋转至其标题

[英]Rotating player towards their heading

I'm trying to replicate a movement system from an old flash game I'm basing a game I'm making in unity on (just for practice, mostly.) The main problem I have is that the player doesn't 'look' towards where he is going. 我正在尝试从一个旧的Flash游戏复制一个运动系统,而这个Flash游戏是我正在统一开发的游戏(大多数情况下只是为了练习)。我遇到的主要问题是玩家不会“看”走向他要去的地方。 He just looks in the direction all the time. 他一直都朝着方向看。

Here's what mine currently looks like 这是我目前的样子

and here's what I want it to look like . 这就是我想要的样子

I've tried doing it but I couldn't find a solution that worked nicely (and the code for it is long gone). 我已经尝试过这样做,但是找不到一个效果很好的解决方案(并且代码已经消失了)。 If someone could help me implement such a thing, that would be great! 如果有人可以帮助我实现这样的事情,那就太好了!

The code for my player control: 我的播放器控件的代码:

public void PlayerFixedUpdate () {
    if (Input.GetKey(KeyCode.A) && transform.position.x > -44){
        if (body.velocity.x > -15){
            body.AddForce(new Vector2(-15,0),ForceMode2D.Force);
        }
    }
    if (Input.GetKey(KeyCode.D) && transform.position.x < 9){
        if (body.velocity.x < 15){
            body.AddForce(new Vector2(15,0),ForceMode2D.Force);
        }
    }
    if (Input.GetKey(KeyCode.W) && transform.position.y < 24){
        if (body.velocity.y < 15*body.mass){
            body.AddForce(new Vector2(0,15),ForceMode2D.Force);
        }
    }
    if (Input.GetKey(KeyCode.S) && transform.position.y > -1){
        if (body.velocity.y > -15*body.mass){
            body.AddForce(new Vector2(0,-15),ForceMode2D.Force);
        }
    }
}

Depends how your object is set up, but one solution would be to set the rotation in the direction of the velocity component. 取决于对象的设置方式,但是一种解决方案是沿速度分量的方向设置旋转。

void rotateFace()
{
   Vector2 dir = rbody.velocity.normalized; // where rbody is your rigidbody2D
   transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(dir.y, dir.x)*Mathf.Rad2Deg - 90); //may not need 90deg offset here
}

You may want to include a check for zero velocity, in which case you should do nothing to maintain the last rotation. 您可能需要包括零速度检查,在这种情况下,您不应该做任何事情来维持最后的旋转。

第二个链接显示他们总是在一个方向上移动,而这恰恰是角色所面对的方向,基本上他们要做的就是使用玩家输入来旋转角色,并继续沿玩家的本地前进方向施加速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM