Hello,i am a young unity user:)
First, I want to explain, that I’m not very familiar with the unity script language:(
I’m working on an FPS game, just to learn and then I’ll try to make a real game
I’m not sure if this is the correct section, please tell me to move the thread
If you want to help me, you are free to help.
I just made a script that handles animations and shooting so when I click I’m going to shoot:sunglasses:
In addition, I made an AI that follows the player but, to avoid that the AI always hits the player, I made it always shoot forward so that when I rotated towards the player I tried to shoot it. But I have a problem, I configured in the script of shot, that could control the rate of fire, but when I do not shoot for a while, shoot very quickly, without using the rate of fire that I assigned.
But with the AI, there is no problem.
If you are interested in helping me improve my prototype first person shooter, I would appreciate the help.
If you ask me, I will give you a download link (sorry if I speak English badly, it is not my main language)
Turret code (I used AI character control from standard assets for movement and rotation)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class turret : MonoBehaviour {
int interval = 1;
public Object Bullet;
float nextTime = 0;
// Update is called once per frame
void Update () {
if (Time.time >= nextTime) {
Instantiate (Bullet, this.transform.position, this.transform.rotation);
nextTime += interval;
}
}
}
HP Script for ai and other objects
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HPScript : MonoBehaviour {
public float hp;
public string bullettag;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Checks if object is dead
if (hp < 0 || hp == 0) {
Destroy (this.gameObject);
}
}
void OnTriggerEnter(Collider coll){
//damage function
if (coll.gameObject.tag == bullettag) {
hp = hp - 1;
}
}
}
Main code assigned to fps controller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEditor;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class FPSAnimationControl : MonoBehaviour {
public float interval = 1;
public float nextTime = 0;
public Animation Anim;
public AnimationClip Idle;
public AnimationClip Run;
public AnimationClip Attack;
public float AnimFadeLength;
public float AnimTargetWeight;
public string IdleString;
public string RunString;
public string AttackString;
public string damagetag;
public RaycastHit HIT;
public Transform BulletOutPoint;
public Vector3 punchoffset;
public Object PunchEffector;
public int layid = 9;
public Object Ball;
public float ballduration;
public Color raycolor;
public float firedelay;
public Light FireLight;
public TextMesh AmmoTXT;
public TextMesh HPTXT;
public float HP = 1;
public float maxHP;
public bool playreload;
public bool playfire;
public float ammo;
public bool reloading;
public float maxammo;
public float mags;
public AudioSource FireSRC;
public AudioSource ReloadSRC;
public Camera MainCam;
public Image HurtIMG;
public float alpha;
public bool debug;
// Use this for initialization
void Start () {
HP = maxHP;
HurtIMG.canvasRenderer.SetAlpha (0);
}
// Update is called once per frame
void Update () {
if (debug == true) {
}
//checks death
if (HP < 0 || HP == 0) {
SceneManager.LoadScene (0);
}
FireLight.enabled = false;
HurtIMG.canvasRenderer.SetAlpha (alpha);
if (Input.GetMouseButton (1) == true) {
MainCam.fieldOfView = 35;
} else {
MainCam.fieldOfView = 60;
}
AmmoTXT.text = ammo + "/" + mags;
HPTXT.text = HP + "%";
if (reloading == true) {
playreload = true;
FireLight.enabled = false;
if (mags > 0) {
AmmoTXT.text = "Reloading...";
ammo = ammo + 1f;
mags = mags - 1f;
}
}
if (mags < 0 || mags == 0) {
reloading = false;
}
if (ammo > maxammo) {
reloading = false;
ammo = ammo - 1;
mags = mags + 1f;
}
if (Input.GetAxisRaw ("Vertical") != 0) {
Anim.clip = Run;
Anim.Blend (RunString, AnimTargetWeight, AnimFadeLength);
} else {
Anim.clip = Idle;
Anim.Blend (IdleString, AnimTargetWeight, AnimFadeLength);
}
if (Input.GetMouseButton (0) == true) {
if (ammo > 0 && reloading == false) {
if (Time.time >= nextTime) {
nextTime += interval;
if (Input.GetMouseButton (0) == true) {
if (ammo > 0 && reloading == false) {
Shoot ();
}
}
if (ammo < 0 || ammo == 0) {
reloading = true;
}
}
if (Input.GetMouseButton (0) == false) {
FireLight.enabled = false;
}
//Sound
if (playreload == true) {
ReloadSRC.Play ();
playreload = false;
}
if (playfire == true) {
FireSRC.Play ();
playfire = false;
}
if (alpha < 0) {
alpha = 0;
}
}
}
}
void LateUpdate(){
alpha = alpha - 1;
}
IEnumerator WAIT(){
yield return new WaitForSeconds (firedelay);
}
void OnTriggerStay (Collider coll)
{//Collect magazine
{
if (coll.gameObject.tag == "mun") {
mags = mags + 100;
Destroy (coll.gameObject);
}
//Damage
if (coll.gameObject.tag == damagetag) {
alpha = 255;
HP = HP - 1;
HurtIMG.canvasRenderer.SetAlpha (alpha);
}
}
}
void Shoot(){
Instantiate (PunchEffector, this.transform.position, this.transform.rotation);
FireLight.enabled = true;
Anim.clip = Attack;
Anim.Blend (AttackString, AnimTargetWeight, AnimFadeLength);
ammo = ammo - 1;
playfire = true;
}
}
Thanks for reading
:);):p:roll_eyes: