- Home /
How to shoot to where "Mouse" is pointing?
Hello,
This is my third day in Unity. I successlly created a small game, as described in my book. The game is "shooting at bricks". Following is the code.
using UnityEngine;
using System.Collections;
public class Shooter : MonoBehaviour {
public Rigidbody bullet;
public float power = 1500f;
public float moveSpeed = 25f;
// Update is called once per frame
void Update ()
{
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(h,v,0);
if(Input.GetButtonUp("Fire1"))
{
Rigidbody instance = Instantiate(bullet,transform.position,transform.rotation) as Rigidbody;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd*power);
}
}
}
Now, the problem is, this is not shooting to where the "Mouse Pointer" is pointing. Instead, it simply start shooting from the center of the screen.
The "Shooter" is assigned to the "Main Camera" object. So, how can I shoot to the place where the mouse pointer is pointing?
Please help
How you code this will depend on whether you want to shoot from the mouse, or whether you want to use the mouse to target something but shoot from some other point.
i know this isn#t the issue you are having, but why are you using GetButtonUp
for shooting?
Answer by MarkD · Oct 20, 2013 at 04:01 PM
This is fairly easy. change Vector3 fwd = transform.TransformDirection(Vector3.forward); to Vector3 fwd = transform.TransformDirection(Vector3.forward) to Vector3 fwd = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset - new Vector3(0,0, - Distance);
Distance is a seperate var as we can only retrive x an y coordinates from the mouse, so the z axis has to be put in manualy.
so the code would looke something like this
using UnityEngine;
using System.Collections;
public class Shooter : MonoBehaviour {
public Rigidbody bullet;
public float power = 1500f;
public float moveSpeed = 25f;
public float Distance=2;
// Update is called once per frame
void Update ()
{
float h = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed;
float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
transform.Translate(h,v,0);
if(Input.GetButtonUp("Fire1"))
{
Rigidbody instance = Instantiate(bullet,transform.position,transform.rotation) as Rigidbody;
Vector3 fwd = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset - new Vector3(0,0, - Distance);
instance.AddForce(fwd*power);
}
}
}
I hope this helps you get along.
What are curScreenPoint and offset, as they're not declared variables in this script, and without them, this code only seems to shoot in one direction, not even taking into account the camera's rotation. (If I look the other way, it's shooting out behind me, ins$$anonymous$$d of out in front of me.) thanks
Your answer
Follow this Question
Related Questions
My bullet wont move forward 1 Answer
How to properly convert mouse pos to world pos 1 Answer
newbie: drag a rigidbody with mouse 2 Answers
Listening to the "Mouse Scroll Wheel" 1 Answer
Array Problem - Error Code BCE0022 1 Answer