- Home /
Top down rotate towards mouse
I am currently working on a 2d game in a top down perspective and I can't figure out how to get bullets to shoot towards the mouse. I've done the obvious of translating screen position to world position but any method I use to rotate the bullets transform.rotate isn't doing anything. I've been looking up information on Quaternions, EulerAngles and Vector3 rotation but I've come up with nothing so far.
Think twin stick shooter but with wasd (or arrow keys) as movement and the mouse is the direction the player shoots. Any help would be much appreciated, I've been at this for a long time now.
If you could provide some of your code to show what you have done, what specifically is not working and what you expected the code to do, we could probably help you a little better.
Sorry I left out the most important bit.
using UnityEngine;
using System.Collections;
public class PlayerShoot : $$anonymous$$onoBehaviour {
public Rigidbody2D Bullet;
public float speed = 20f;
void Update() {
if(Input.GetButtonDown("Fire1")) {
Vector3 myPos = transform.position;
float x = Input.mousePosition.x;
float y = Input.mousePosition.y;
Vector3 direction = Camera.main.ScreenToWorldPoint
(new Vector3(x, y, transform.position.z)) - myPos;
Rigidbody2D bulletClone = (Rigidbody2D)Instantiate
(Bullet, myPos, Quaternion.LookRotation(direction));
}
}
}
As for angry robots I don't seem to have it on my machine as a demo project from the latest install of Unity, is there an alternate method to download it? From what I played it's in a 3D space, yet any methods I've tried to apply to a 2D space haven't worked.
Answer by anniyan137 · Jan 08, 2014 at 11:40 AM
Try looking at the sample project Angry bots, that is given with Unity. I think you will find what you're looking for in there.
Answer by robertbu · Jan 08, 2014 at 05:56 PM
'Top Down' to me means the camera looking down the 'Y' axis onto the XZ plane. This appears to be a standard view with the camera looking towards positive 'Z' onto the XY plane. One problem that may be what is causing your issues is on line 5. The 'Z' parameter is the distance in front of the camera, not the position of your object. Assuming your 2D plane has a 'z' of 0.0, and the setup is as I assume, you can just negate the value:
Vector3 direction = Camera.main.ScreenToWorldPoint
(new Vector3(x, y, -Camera.main.transform.position.z)) - myPos;
If the 2D plane has a 'Z' position other than 0.0, you can do:
Calculate the position of the 'Z' by:
float dist = transform.position - Camera.main.transform.position.z;
Note if the distance from the camera to the 2D plane remains constant, you can do either calculation in Start().
Also when you do the look rotation, you likely want to specify the optional second (up) parameter. Given a 2D game with the camera looking forward, you want to use Vector3.forward for this prameter.
Quaternion.LookRotation(direction, Vector3.forward);
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to convert to 2D 1 Answer
2D PlatformerController: Cumulative drift? 1 Answer
2d Game Jump Issue 2 Answers
Problem with playmaker gui and "Get mouse button down" action. 1 Answer