Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使2d精灵沿按键方向以平滑的宽弧移动?_C#_Unity3d_2d_Game Physics - Fatal编程技术网

C# 如何使2d精灵沿按键方向以平滑的宽弧移动?

C# 如何使2d精灵沿按键方向以平滑的宽弧移动?,c#,unity3d,2d,game-physics,C#,Unity3d,2d,Game Physics,我在Unity做一个小实验项目。我有一个2d精灵,它以一定的速度向前移动,但我希望它以一个宽的弧线向左或向右旋转,并在按键时继续朝那个方向移动 我试着调整它的角速度来获得想要的效果。看起来不自然,它不会停止旋转 我试过勒平。看起来也不自然 代码片段1: bool forward = true; Vector3 movement; void FixedUpdate() { if (forward) { //Moves forward movemen

我在Unity做一个小实验项目。我有一个2d精灵,它以一定的速度向前移动,但我希望它以一个宽的弧线向左或向右旋转,并在按键时继续朝那个方向移动

  • 我试着调整它的角速度来获得想要的效果。看起来不自然,它不会停止旋转
  • 我试过勒平。看起来也不自然
  • 代码片段1:

    bool forward = true;
    Vector3 movement;
    
    void FixedUpdate()
    {
        if (forward)
        {
            //Moves forward
            movement = new Vector3(0.0f, 0.1f, 0.0f);
            rb.velocity = movement * speed;
        }
    
    
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            forward = false;
            movement = new Vector3(-0.05f, 0.05f, 0.0f);
            rb.velocity = movement * speed;
            rb.angularVelocity = 30;
        }
    
        if (transform.rotation.z == 90)
        {
            movement = new Vector3(-0.1f, 0.0f, 0.0f);
            rb.velocity = movement * speed;
            rb.angularVelocity = 0;
        }
    
    }
    
    代码段2:

    void Update(){
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
        Vector3 target = transform.position + new Vector3(-0.5f, 0.5f, 0);  
        transform.position 
        =Vector3.Lerp(transform.position,target,Time.deltaTime);
        transform.eulerAngles = Vector3.Lerp(transform.rotation.eulerAngles, 
        new Vector3(0, 0, 90), Time.deltaTime);
        }
    }
    

    有人能告诉我实现这一点的正确方法吗?

    不完全确定这是否是你想要实现的,但这里有一些我想让你开始的伪代码

    基本上,当一个方向被按下时,你想要在那个方向上增加速度,直到所有的速度都指向那个方向。同时,你要降低你先前前进方向上的速度,直到它为零

    然而,这是一个简化的公式-如果你真的想让速度在整个弧中保持恒定,你必须使用一些几何体,知道V=(velX^2+velY^2)^.5,但这会让你非常接近

    float yvel = 1f, xvel;
    float t;
    
    void Update()
    {
        GetComponent<Rigidbody2D>().velocity = new Vector2(xvel, yvel);
    
        t += Time.deltaTime;
        if (Input.GetKeyDown(KeyCode.D))
        {
            t = 0;
            StartCoroutine(Move());
        }
    }
    
    private IEnumerator Move()
    {
        while (t < 2) // at time t, yvel will be zero and xvel will be 1
        {
            yvel = 1 - .5f * t; // decrease velocity in old direction
            xvel = .5f * t; // increase velocity in new direction
            yield return null;
        }
    }
    
    float yvel=1f,xvel;
    浮动t;
    无效更新()
    {
    GetComponent().velocity=新矢量2(xvel,yvel);
    t+=时间增量时间;
    if(Input.GetKeyDown(KeyCode.D))
    {
    t=0;
    start例程(Move());
    }
    }
    私有IEnumerator移动()
    {
    而(t<2)//在时间t时,yvel为零,xvel为1
    {
    yvel=1-.5f*t;//降低旧方向的速度
    xvel=.5f*t;//在新方向上增加速度
    收益返回空;
    }
    }