0

I am trying to make 2D top-down shooter, and I can't figure out how to do a raycast. 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. 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!

Steven
  • 166,672
  • 24
  • 332
  • 435
benh1213
  • 3
  • 1
  • 2
  • 1
    Normally direction would be `mouse position - player position`. This results in a direction vector. – Emond Sep 27 '14 at 20:05

2 Answers2

0

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

// 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.

maZZZu
  • 3,585
  • 2
  • 17
  • 21
0

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. However if using NGUI you have the camera script you can attach to any camera, including the main one. This script then can let such camera be a listener of events that NGUI tracks in accordance. 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.

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

Hope it can still be of help. Even if dependent on a 3rd party library.

  • If I understood correctly, IronEagle13 was going to use rays for testing which game object a shoot hits. For that you cannot use NGUI. – maZZZu Sep 28 '14 at 05:18
  • sorry I thought IronEagle13 wanted to learn if there was an object right below the mouse pointer (like a cross hair for a target, since top-down). Mine bad. Then it doesn't work. – CodeAssembler Sep 29 '14 at 00:19