Question by
Fabjanczyk · Jul 24, 2020 at 05:56 PM ·
scripting problemshootingbegginer
Please help me with my shooting script.
I keep getting error message: "cannot convert from method group to string" or that string emiterName doesn't exist. I tried many different things and i can't get this to work. Could someone help me and explain my mistakes?
When in void Update() the starcouroutine is // and shooting isn't // (and delayedShooting is "commented out") the script works but not as i want it to (no delay).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
public Transform bulletTransform;
public GameObject bullet;
public float bulletSpeed;
float shotTimer;
float shotTimerElapsed;
public float shotsPerSecond;
public GameObject emiter;
void Start()
{
shotTimer = 1 / shotsPerSecond;
shotTimerElapsed = 0;
}
void Update()
{
shotTimerElapsed = shotTimerElapsed + Time.deltaTime;
if (Input.GetMouseButton(0) && shotTimerElapsed >= shotTimer )
{
StartCoroutine(delayedShooting);
//Shooting();
}
}
IEnumerator delayedShooting(float time)
{
string emiterName = emiter.name;
if (emiterName == "emiterR_mech")
{
Shooting();
}
if (emiterName == "emiterL_mech")
{
yield return new WaitForSeconds(0.25f);
Shooting();
}
}
void Shooting()
{
//shotTimerElapsed = shotTimerElapsed + Time.deltaTime;
Quaternion rotation = bulletTransform.rotation;
if (Input.GetMouseButton(0) && shotTimerElapsed >= shotTimer)
{
shotTimerElapsed = 0;
GameObject instBullet = Instantiate(bullet, transform.position, rotation) as GameObject;
Rigidbody instBulletRigidbody = instBullet.GetComponent<Rigidbody>();
instBulletRigidbody.AddRelativeForce(Vector3.forward * bulletSpeed);
Destroy(instBullet, 3f);
}
if (Input.GetMouseButtonUp(0))
{
shotTimerElapsed = shotTimer;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Problem with a shooting script. 0 Answers
Trying to have bullets travel in the direction the player is facing 0 Answers
2 Guns aren't meant to fire together but they do 0 Answers
Instantiate And move the GameObject to the click position from another position. 1 Answer
How to make a shooting bow? 0 Answers