简体   繁体   English

Unity 2D自上而下的光线投射

[英]Unity 2D Top-Down Raycasting

I am trying to make 2D top-down shooter, and I can't figure out how to do a raycast. 我正在尝试制作自上而下的2D射击游戏,但我不知道该如何进行光线投射。 I don't know what to put for the second parameter- the direction. 我不知道要为第二个参数(方向)输入什么。 I've tried Input.mousePosition, and some other things I've seen on StackOverflow, but I can't seem to make anything work. 我已经尝试过Input.mousePosition以及在StackOverflow上看到的其他一些东西,但是我似乎无法使任何事情起作用。 Here is my code on my player character. 这是我关于玩家角色的代码。

//Raycasts
var forward = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
Debug.DrawRay (transform.position, forward * 10, Color.green);

//Mouse Movement
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle - 90));


//Player Movement
if (Input.GetKey (KeyCode.D)) {rigidbody2D.AddForce(Vector3.right * speed);}
if (Input.GetKey (KeyCode.A)) {rigidbody2D.AddForce(Vector3.left * speed);}
if (Input.GetKey (KeyCode.W)) {rigidbody2D.AddForce(Vector3.up * speed);}
if (Input.GetKey (KeyCode.S)) {rigidbody2D.AddForce(Vector3.down * speed);}

Is there something I can reuse from my Mouse Movement part of the script to do the direction part of the raycast? 我可以从脚本的“鼠标移动”部分重用某些内容来做光线投射的方向部分吗?

Thank you so much for the help! 非常感谢你的帮助!

It could be a good idea to get to know some vector math for game programming. 了解游戏编程的一些矢量数学可能是一个好主意。

In your case what you want is a vector that is pointing from the player position to the mouse position. 在您的情况下,您想要的是一个从玩家位置指向鼠标位置的向量。 You can compute this as Erno de Weerd mentioned in comments by calculating 您可以通过在注释中提到的Erno de Weerd来计算

// shootDirection = mousePosition - playerPosition
Vector3 shootDirection = Input.mousePosition - transform.position;

This is equivalent of moving backwards from player position to origin and forward from origin to mouse position. 这等效于从玩家位置向原点后退和从原点到鼠标位置前移。

This answer might not be directly related to your question but I figure I told anyways in case it might still be of help. 这个答案可能与您的问题没有直接关系,但我想我还是告诉过您,以防仍然有帮助。

I don't know about the new Unity GUI (UGUI), it might have similar support. 我不知道新的Unity GUI(UGUI),它可能有类似的支持。 However if using NGUI you have the camera script you can attach to any camera, including the main one. 但是,如果使用NGUI,则可以使用摄像机脚本,可以将其附加到任何摄像机,包括主摄像机。 This script then can let such camera be a listener of events that NGUI tracks in accordance. 然后,此脚本可以使此类摄像机成为NGUI跟踪的事件的侦听器。 If you make such camera to 'listen to world' by checking such option, then you can just put a small script like this in any game object with a collider set as trigger and it would respond automatically for you. 如果您通过选中此选项使此类摄像机“监听世界”,则只需将像这样的小脚本放在将对撞机设置为触发器的任何游戏对象中,它将自动为您响应。 So effectively you don't have to make use of manual raycasting anymore to detect such click or touch. 因此,有效地,您不必再使用手动射线广播来检测这种单击或触摸。

NOTE: Requires NGUI plugin and with NGUI camera script attached to camera, the event listener set to world and the gameObject that has the script needs to have a collider set as trigger on it. 注意:需要NGUI插件,并且在相机上附加了NGUI相机脚本,将事件侦听器设置为world,并且具有该脚本的gameObject需要将对撞机设置为触发器。

  void Onclick () { Debug.Log("Hey! you clicked me!"); } 

Hope it can still be of help. 希望它仍然可以有所帮助。 Even if dependent on a 3rd party library. 即使依赖于第三方库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM