- Home /
Shooting bullet towards mouseposition 2D.
Ive been trying to piece out how to fire a bullet in the direction of the mouse, but the bullet goes no where.
GameObject energyBall = Instantiate (Prefabs.EnergyBall) as GameObject;
energyBall.transform.position = transform.position;
var mousePos = Input.mousePosition;
mousePos.z = (transform.position.z - Camera.mainCamera.transform.position.z); //The distance from the camera to the player object
var worldMousePosition = Camera.main.ScreenToWorldPoint (mousePos);
energyBall.transform.LookAt (worldMousePosition, Vector3.forward);
energyBall.rigidbody2D.AddForce (Vector3.forward * 5000);
Answer by robertbu · Jan 10, 2014 at 11:21 PM
You've got two problems here. The first is that Rigidbody2D operates on the XY plane. It will ignore any force added in the Z direction. In fact it takes a Vector2, so the 'Z' component of a Vector3 is tossed away. So the use of Vector3.forward (which is (0,0,1)) will do nothing in a Rigidbody2D.AddForce() call. The second likely problem is how you've handled the rotation code. For a 2D sprite, you don't want to face the 'Z' direction towards the LookAt(). Instead you want whatever side of the sprite represents forward to be pointed at the correct direction. Here is a bit of (untested) code. Replace your code starting with line 6:
var worldMousePosition = Camera.main.ScreenToWorldPoint (mousePos);
var direction = worldMousePosition - transform.position;
var angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Atan2;
energyBall.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
energyBall.rigidbody2D.AddForce (energyBall.transform.right * 5000);
This code assumes the 'forward' of your sprite is the right side. It would take a bit of fixup if the 'forward' of your sprite is the 'up' side.
Thanks for answering. I tried your code and what happens now is that the ball fires to the right. If i use Vector3.Left in the AddForce it fires to the left. But mouseposition does not affect the direction.
The last line needs to be changed to 'transform.right' rather than 'Vector3.right'. I changed the example code.
This is what i got now. Its only shooting to the right. GameObject energyBall = Instantiate (Prefabs.EnergyBall) as GameObject; energyBall.transform.position = transform.position; var mousePos = Input.mousePosition;
mousePos.z = (transform.position.z - Camera.mainCamera.transform.position.z); //The distance from the camera to the player object var world$$anonymous$$ousePosition = Camera.main.ScreenToWorldPoint (mousePos); var direction = world$$anonymous$$ousePosition - transform.position; var angle = $$anonymous$$athf.Atan2 (direction.y, direction.x); transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward); energyBall.rigidbody2D.AddForce (transform.right * 5000);
Need to rotate the energy ball (fixed my code):
energyBall.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Ok i changed it, but no luck. :(
GameObject energyBall = Instantiate (Prefabs.EnergyBall) as GameObject;
energyBall.transform.position = transform.position;
var mousePos = Input.mousePosition;
mousePos.z = (transform.position.z - Camera.mainCamera.transform.position.z); //The distance from the camera to the player object
var world$$anonymous$$ousePosition = Camera.main.ScreenToWorldPoint (mousePos);
var direction = world$$anonymous$$ousePosition - transform.position;
var angle = $$anonymous$$athf.Atan2 (direction.y, direction.x);
energyBall.transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
energyBall.rigidbody2D.AddForce (transform.right * 5000);
Answer by BudBroesky · Aug 04, 2014 at 02:43 AM
So for 2D games I've found to get the world position of the mouse is really simple! Just use the code below! The only issue with this code is that when the bullet reaches the mouse it stops... i quickly came up with this code and am going to to figuring out how to fix it. I'm assuming it has something to do with the "Vector2.MoveTowards" part.
using UnityEngine; using System.Collections;
public class BulletBehaviors : MonoBehaviour {
public Vector2 mousePosition;
public float bulletSpeed;
private float step;
// Use this for initialization
void Start () {
mousePosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
}
// Update is called once per frame
void Update () {
step = bulletSpeed * Time.deltaTime;
transform.position = Vector2.MoveTowards (transform.position, mousePosition, step);
}
}
Your answer
Follow this Question
Related Questions
AddForce at mouse position only on table area 0 Answers
Rotating object attached to a rolling ball... 1 Answer
LookAt but using physics AddRelativeTorque 1 Answer
Using Main Camera with transform.LookAt at a target but no rotation of camera 1 Answer
I need to add force based on the angle I'm looking at the object. 1 Answer