首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发射投射物的方向与我的角色所看的方向相同。[UNITY 2D TOPDOWN]

发射投射物的方向与我的角色所看的方向相同。[UNITY 2D TOPDOWN]
EN

Stack Overflow用户
提问于 2015-04-23 22:13:16
回答 1查看 1.6K关注 0票数 0

我搜索了一堆关于自上而下2D旋转和发射导弹的帖子,没有一个适用于我的帖子。

我的巫师设法从他的魔杖上发射了他的魔法导弹,但它们与他的魔杖的方向不一致。

它应该从魔杖的四元数中获取Z值,并指定它的角度(或者至少我认为它应该这样做),但它看起来比魔杖旋转得更快,虽然它确实改变了,但如果我旋转它,它不会随着魔杖改变。所以,如果我指向上面,它就会被点燃。如果我指向右边45度,它就会直接向我的巫师发射导弹。

MissileMovement代码

代码语言:javascript
复制
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);
 }
}

字符移动代码

代码语言:javascript
复制
 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); 


         }
     }             
 }

如果有人能告诉我哪里出了问题,那就太好了。:)

EN

回答 1

Stack Overflow用户

发布于 2015-04-23 23:57:59

所以它不工作的原因是因为它读取方向,然后添加更多的内容,所以它有点过度校正。因此,代码应为:

代码语言:javascript
复制
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);
 }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29826091

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档