我搜索了一堆关于自上而下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);
}
}
}如果有人能告诉我哪里出了问题,那就太好了。:)
发布于 2015-04-23 23:57:59
所以它不工作的原因是因为它读取方向,然后添加更多的内容,所以它有点过度校正。因此,代码应为:
using UnityEngine;
using System.Collections;
public class MoveMissile : MonoBehaviour {
// Use this for initialization
public float speed = 0.5F;
public Transform Shotspawn;
void Start (){
// Sets the direction that shot is fired in.
transform.rotation=Shotspawn.rotation;
}
// Update is called once per frame
void Update () {
transform.Translate(Vector2.up * speed);
}
}https://stackoverflow.com/questions/29826091
复制相似问题