- Home /
Third Person Shooting Script Stopped Working
So about a week ago I made a shooting script for my third person shooter,
using UnityEngine;
using System.Collections;
public class ShootingScript : MonoBehaviour {
public Transform bulletSpawn;
RaycastHit hit;
public float range;
public Rigidbody bullet;
public float force;
public Transform raycastStart;
Vector3 foundHit;
// Update is called once per frame
void Update () {
bulletSpawn.LookAt(foundHit);
Raycast();
if(Input.GetMouseButtonDown(1))
{
Shoot();
}
}
void Shoot()
{
Rigidbody clone;
clone = Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation) as Rigidbody;
clone.velocity = bulletSpawn.transform.TransformDirection(Vector3.forward * force);
}
void Raycast()
{
if(Physics.Raycast(raycastStart.transform.position,raycastStart.transform.forward, out hit, range))
{
foundHit = hit.point;
}
}
}
and when I played my game today, the bullet wasn't spawning. Bullet Spawn is still looking at the foundhit point, but when i hit left mouse, no bullet comes out. So I know that the problem is in the Shoot() void, but I can't find anything wrong. There is nothing in the console comin up about this and all variables are assigned. Can anyone help me?
Answer by SolutionStudios · Jul 17, 2015 at 10:47 AM
Line 20:
if(Input.GetMouseButtonDown(1))
uses the right mouse button.
For left mouse use: if(Input.GetMouseButtonDown(0))
Also, are lines 18 and 19 the wrong way around? Shouldn't you call Raycast() first to find the foundHit position and then make the spawn object look at the new position?
Hey thanks. I feel so stupid for not seeing that XP! Turns out it was working perfectly, i was just hitting the wrong button. Thanks Again!