Shooting at mouse position third person camera
Hi! I have a third person camera and want to shoot a bullet in mouse direction, right now my bullet only shoots in front of my block, how do I shoot in mouse direction? thats my Code for the playercontroller:
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
private float nextFire = 0.0f;
void Update ()
{
if (Input.GetKey (KeyCode.Mouse1) && Time.time > nextFire && Menu==false) {
nextFire = Time.time + fireRate;
Instantiate (shot, shotSpawn.position, shotSpawn.rotation);
and I added a velocity script to my bullet:
public float mspeed;
void Start () {
GetComponent<Rigidbody> ().velocity = transform.forward * mspeed;
}
Can someone please explain how I change the transform to the Mouse position please?
Answer by Abhiroop-Tandon · Mar 23, 2016 at 03:26 PM
You just need to instantiate the object at the camera position with some velocity, something like
newProjectile = Instantiate(projectile, Camera.main.transform); (something like this)
this wont move the camera. what do you want then ??
You said you wanted to instantiate the object at the camera transform, but that would mean it would shoot directly in front of where my camera is looking right? Sry for my broken english.. EDIT: $$anonymous$$y camera is fixed behind my character without a script, I just made it a child of my character, I can only move with WASD and Q and E and right now my mouse is completely not in use for anything