方向实体在统一2d中运动故障

问题描述 投票:0回答:1

我正在Unity 2D中创建一个游戏。我有龙,我正在添加到场景中。龙只应该向上,向下,向左和向右四个方向中的一个方向移动。左右移动的龙完全按预期移动。然而,上下移动的龙有一个问题,即它们以一定角度移动。所有向上移动的龙都以45度角向上和向右移动。向下移动的所有龙以45度角向下和向左移动。起初我认为动画师将龙移动到不同的位置是一个问题,但是我从预制件中移除了动画组件并且问题仍然存在。

下面是我用来移动龙的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DragonMovment : MonoBehaviour {
    public string Direction;    //needs to be set in the prefab

    public float DragonSpeed;   //speed of dragon

    Rigidbody2D rb; 

    public Transform Boundries;

    // Use this for initialization
    void Start ()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate ()
    {
        float MoveRight = 1;
        float MoveLeft = -1;
        float MoveUp = 1;
        float MoveDown = -1;

        if (Direction== "R")
        {
            rb.velocity = new Vector3(DragonSpeed * MoveRight, rb.velocity.y);
        }
        if (Direction == "L")
        {
            rb.velocity = new Vector3(DragonSpeed * MoveLeft, rb.velocity.y);
        }
        if (Direction == "U")
        {
            rb.velocity = new Vector3(DragonSpeed * MoveUp, rb.velocity.x);
        }
        if (Direction == "D")
        {
            rb.velocity = new Vector3(DragonSpeed * MoveDown, rb.velocity.x);
        }


    }
}

编辑。那么为什么以下工作呢?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerControler : MonoBehaviour {
    // speed of movment 
    public float Speed;
    // rb
    Rigidbody2D rb;


    public Transform Boundries;

    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        // get horizontal input
        float MoveSide = Input.GetAxis("Horizontal");
        //get vertical input
        float MoveVert = Input.GetAxis("Vertical");
        // move horizontal
        rb.velocity = new Vector3(Speed * MoveVert, rb.velocity.y);
        // move vertical 
        rb.velocity = new Vector3(Speed * MoveSide, rb.velocity.x);
    }
}

但其他代码没有?

c# unity3d direction
1个回答
1
投票

你在向量3的y中得到了速度x

if (Direction == "U")
{
    rb.velocity = new Vector3(rb.velocity.x, DragonSpeed * MoveUp);
}
if (Direction == "D")
{
    rb.velocity = new Vector3(rb.velocity.x, DragonSpeed * MoveDown);
}

当您覆盖后续语句中的值时,它适用于您的播放器脚本。

    float MoveSide = Input.GetAxis("Horizontal"); //eg 1
    float MoveVert = Input.GetAxis("Vertical"); // eg 1

    // setting your x velocity incorrectly to the y (vert) velocity speed and keeping y the same velocity as start of frame
    rb.velocity = new Vector3(Speed * MoveVert, rb.velocity.y);
    // Set the y to the x value of the statement above so it is now in the correct vector and set the x to the correct hoz velocity
    rb.velocity = new Vector3(Speed * MoveSide, rb.velocity.x);

// effectively doing 
rb.velocity = new Vector3(Speed * MoveSide, Speed * MoveVert);

您还应该使用MovePosition,因为它不会直接影响物理引擎(使用速度可以对碰撞和触发产生爆震效应并创建意外的物理)。你的游戏对象必须被标记为运动学,否则下面将使他们立即传送到新位置。

var movementDirection = new Vector3(Speed * MoveSide, Speed * MoveVert);
rb.MovePosition(transform.position + movementDirection * Time.deltaTime);

并且* Time.deltaTime确保移动对于不同的帧速率是一致的。如果您在30 fps的机器上运行游戏,游戏对象的移动速度将低于60 fps。 Time.deltaTime计算自上一帧以来经过的物理时间,并确保无论帧速率如何,行进的距离都相同。

例如,游戏对象每帧更新移动1次。在30 fps机器上经过一秒后,物体将移动30.在60 fps机器上经过一秒后,物体将移动60。

Time.deltaTime=.2s on 30 fps so 1 movement * .2 = move .2 per frame * 30 frames in the second = 60 moved
Time.deltaTime=.1s on 60 fps so 1 movement * .1 = move .1 per frame * 60 frames in the second = 60 moved
© www.soinside.com 2019 - 2024. All rights reserved.