- Home /
Scripting relating to FIRST PERSON SHOOTER
I have done some scripting relating to FIRST PERSON SHOOTER and i am stuck in scripting I need you guys to help me. I am making the game of first person shooter, while making it script i saw and error in 3rd line after void update function "StartCoroutine (ShotEffect());" this ShotEffect is the function below and i want it to call and then this error appears, I haven't take any step to diagnose it because i don't know how to deal with that problem. The problem is that the function ShotEffect is not calling and giving error. The function ShotEffect is about the particle that has to be appear when gun is fired and have to destroy in few seconds.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class RaycastShootComplete : MonoBehaviour {
public int gunDamage = 1;
public float fireRate = 0.25f;
public float weaponRange = 50f;
public float hitForce = 100f;
public Transform gunEnd;
public GameObject Particle;
private Camera fpsCam;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
private AudioSource gunAudio;
private LineRenderer laserLine;
private float nextFire;
//Use this for initialization
void Start() {
laserLine = GetComponent<LineRenderer>();
gunAudio = GetComponent<AudioSource>();
fpsCam = GetComponentInParent<Camera>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
StartCoroutine (ShotEffect());
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
RaycastHit hit;
laserLine.SetPosition(0, gunEnd.position);
if (Physics.Raycast(rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
{
laserLine.SetPosition(1, hit.point);
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * hitForce);
}
}
else
{
laserLine.SetPosition(1, rayOrigin + (fpsCam.transform.forward * weaponRange));
}
}
}
private IEnumerable ShotEffect()
{
this.gameObject.GetComponentInParent<Rigidbody>().AddForce(new Vector3(-50f, 0, 0), ForceMode.Impulse);
GameObject particle = Instantiate(Particle, gunEnd.transform.position, Particle.transform.rotation);
Destroy(particle, 0.5f);
gunAudio.Play();
laserLine.enabled = true;
yield return shotDuration;
laserLine.enabled = false;
}
}
Answer by FoodLover195 · Oct 21, 2018 at 09:58 AM
Hey @fazilmahesania
A common mistake because the names are similar, your function needs to return a type of IEnumerator instead of IEnumerable. Also for next time please include the error line :)
Your answer
Follow this Question
Related Questions
Empty scripts to avoid drag and drop? 2 Answers
Assets/BallControl.js(9,30): UCE0001: ';' expected. Insert a semicolon at the end. 0 Answers
Level Select screen Messed Up - Please help! 0 Answers
Make event stop 3 Answers
Relative AddForce 0 Answers