我已经搜索了一堆关于使用发射导弹的自上而下 2D 旋转的帖子,但没有一个适用于我的。
我的巫师设法用他的魔杖发射了他的魔法导弹,但它们出来时与他的魔杖方向不一致。它应该从魔杖的四元数中获取 Z 值,并将其指定为它发出的角度(或者至少我认为它应该这样做)但它似乎比魔杖旋转得更快,虽然它确实会改变,如果我旋转它并没有与魔杖一样变化。因此,如果我指出它会启动。如果我指向右侧 45 度,它会直接向我的巫师发射导弹。
MissileMovement 的代码 ----------------
using UnityEngine;
using System.Collections;
public class MoveMissile : MonoBehaviour {
// Use this for initialization
public float speed = 0.5F;
public Transform Shotspawn;
// public Quaternion Direction;
private float Direction;
void Start (){
// Sets the direction that shot is fired in.
Direction = transform.rotation.eulerAngles.z;
transform.Rotate(0 , 0, Direction);
}
// Update is called once per frame
void Update () {
transform.Translate(Vector2.up * speed);
}
}
角色移动的代码--------------------------
using UnityEngine;
using System.Collections;
public class TopDownCharController2 : MonoBehaviour {
// Movement Variables
public float walkSpeed;
public bool colliding;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if(Input.GetKey (KeyCode.I))
{transform.Translate(Vector2.up * walkSpeed); } // UP MOVEMENT
if(Input.GetKey(KeyCode.J))
{transform.Translate(-Vector2.right * walkSpeed); } // LEFT MOVEMENT
if(Input.GetKey(KeyCode.K))
{transform.Translate(-Vector2.up * walkSpeed); }// DOWN MOVEMENT
if(Input.GetKey(KeyCode.L))
{transform.Translate(Vector2.right * walkSpeed); }// RIGHT MOVEMENT
if(Input.GetKey(KeyCode.U)) {
// Clockwise
transform.Rotate(0, 0, -3.0f);
}
if(Input.GetKey(KeyCode.O)) {
// Counter-clockwise
transform.Rotate(0, 0, 3.0f);
}
}
}
如果有人能告诉我我哪里出错了,那就太好了。:)