我正在 Unity 2D 中创建游戏。我有要添加到场景中的龙。龙只应该在 4 个方向中的 1 个方向上移动,向上、向下、向左和向右。左右移动的龙完全按照预期移动。然而,上下移动的龙有一个问题,就是它们移动时会倾斜。所有向上移动的龙都以 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>();
}
}
但其他代码没有?
慕无忌1623718
相关分类