- Home /
Unity 2D: move towards mouse and through mouse
Hi, I am a noob, and I have looked everywhere for a script that moves a bullet(or object) towards the mouse and through the mouse position. My game is a 2D top down shooter using the x and y coordinates(basically no gravity). Can anyone point me in the right direction to make my bullets move this way.
First, just a general question. Do you have an overall strategy, i.e. once the bullet is in motion, how do you plan to detect if the bullet hit its target? This is going to be much more complicated that just moving the bullet.
You say you are a noob, so I'll just suggest starting with some tutorials on basic coordinate systems, game object movement, and scripting, but beyond that you'll have to handle user interaction (click a button to fire the bullet), camera interaction (transform the mouse click from screen space to world space), ray casting (to deter$$anonymous$$e what is in the way of the bullet), object rotations, and script management. I understand you want to just jump in and start having fun, but there is a large amount of work/knowledge involved even for a simple thing, sorry, but I would highly recommend the tutorials first. If you search google or even Unity Answers though, there are plenty of scripts available that move objects. Did you look at the "Related Questions" on the right side of this page? There are several that look similar. Good luck, and have fun!
Answer by robertbu · May 20, 2014 at 03:54 PM
Here is the basic code. You need to have a 'prefab' game object variable at the top of the file and an 'amount' variable that defines how much force to apply. Start with 'amount' set to 500, then adjust. Note your sprite should have the forward of your bullet facing right when the rotations is (0,0,0).
var pos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - pos;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
var rotation = Quaternion.AngleAxis(angle, Vector3.forward);
var go = Instantiate(prefab, transform.position, rotation);
go.rigidbody2D.AddForce(transform.right * amount);
For some reason, unity is not detection rotation as anything, do you know the issue? And also, I'm assu$$anonymous$$g I put this in an update function.
There should have been a 'var' in front of 'rotation'. Note this code assumes your bullets have a Rigidbody2D. You're not trying to move them through the Transform are you?
$$anonymous$$y bullets have a rigid body,and whenever I click it creates a new prefab at my players location. Code for clicking:
var thePrefab : GameObject;
function Update () {
if(Input.GetButtonUp("Fire1")){
if($$anonymous$$anaBar.mp > 20){
$$anonymous$$anaBar.mp -= 20;
Instantiate(thePrefab,transform.position, transform.rotation);
//var instance : GameObject =
}
}
Code that you gave me:
var prefab : GameObject;
var amount : float;
var Timer : float;
var Timer$$anonymous$$ax : float;
function Start () {
Timer$$anonymous$$ax = 500;
}
function Update () {
var pos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - pos;
var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
var rotation = Quaternion.AngleAxis(angle, Vector3.forward);
var go = Instantiate(prefab, transform.position, rotation);
go.rigidbody2D.AddForce(transform.right * amount);
Timer += 1;
if(Timer == Timer$$anonymous$$ax){
Destroy(gameObject);
}
}
function OnCollisionStay2D(coll: Collision2D) {
if(coll.gameObject.name == "Enemy"){
Destroy(gameObject);
}
if(coll.gameObject.name == "Wall"){
Destroy(gameObject);
}
}
For some odd reason, whenever I click the game is creating a thousand bullets, to the point of my program crashing. what am I doing wrong?
The code I gave you goes into your first function, not in the bullet code. Something like:
var thePrefab : GameObject;
var amount : float = 500.0;
function Update () {
if(Input.GetButtonUp("Fire1")){
if($$anonymous$$anaBar.mp > 20){
$$anonymous$$anaBar.mp -= 20;
var pos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - pos;
var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
var rotation = Quaternion.AngleAxis(angle, Vector3.forward);
var go = Instantiate(prefab, transform.position, rotation);
go.rigidbody2D.AddForce(transform.right * amount);
}
}
}
the script now works. However, the bullets only shoot to the right ins$$anonymous$$d of all 4 directions. Is there a quick fix to this? I am using the x and y coordinates.
Your answer
Follow this Question
Related Questions
Moving an object on the X axis corresponding to mouse movment 0 Answers
2D: How to make Gradius III with a mouse? 1 Answer
Detect Mouse Movement 1 Answer
OnMouseUp() Click Display Effect. 1 Answer