我正在Unity 2D中创建游戏。我有要添加到场景中的Dragons。龙只应沿4个方向之一移动,即上,下,左和右。左右移动的龙完全按照预期移动。然而,上下移动的龙有一个问题,就是它们成一定角度移动。所有向上移动的龙都以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);
}
}
但是其他代码没有输入吗?
答案 0 :(得分: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计算自上一帧以来经过的物理时间,并确保无论帧速率如何,行进的距离都相同。
例如说gameObject每帧更新移动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