I’m creating a game on Unity with no experience. The problem is that, when coding the behavior of my weapons, there’s an error as it does not let me push the play button and it sends me a notification saying that “if” is invalid in class, struct, or interface member declaration.
Here’s my code I would like you to review:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponBehaviour : MonoBehaviour
{
public GameObject bulletPreFab;
public float speed; // 'float' is just a normal number
public float accuracy;
public float secondsBetweenShots;
float secondsSinceLastShot;
// Start is called before the first frame update
void Start()
{
secondsSinceLastShot = secondsBetweenShots;
}
// Update is called once per frame
void Update()
{
// fire
secondsSinceLastShot += Time.deltaTime;
}
public void Fire(Vector3 targetPosition);
if (secondsSinceLastShot >= secondsBetweenShots)
{
GameObject newBullet = Instantiate(bulletPreFab, transform.position + transform.forward, transform.rotation);
//Offset target position randomly according to inacuracy
float inaccuracy = Vector3.Distance(transform.position, targetPosition) / accuracy;
targetPosition.x += Random.Range(-inaccuracy, inaccuracy);
targetPosition.z += Random.Range(-inaccuracy, inaccuracy);
targetPosition.y += Random.Range(-inaccuracy, inaccuracy);
secondsSinceLastShot = 0;
}
}
Thanks in advance for the help.