- Home /
Question by
fraubolat · Jun 02, 2020 at 07:40 PM ·
scripting problemuishootingraycasting
ui problem
i want to degrees me ui text whenever i fire but its not working here is the script shoot
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class shootak47 : MonoBehaviour
{
public Transform wheretoshoot;
int range = 1000;
bool ishooting = false;
bool isreloading = false;
public int bullet = 30;
public int currentbullet;
const float fireDelay = 0.25f;
float delayTimer = 0;
public GameObject sprite;
enemyhealth health;
void Start()
{
currentbullet = bullet;
}
// Update is called once per frame
void Update()
{
if (delayTimer > 0)
{
delayTimer -= Time.deltaTime;
if (delayTimer < 0) delayTimer = 0;
}
if (Input.GetButton("Fire1"))
{
if (delayTimer == 0)
{
shooting();
delayTimer = fireDelay;
}
}
}
public void shooting ()
{
if (currentbullet == 0) return;
if(!ishooting)
{
RaycastHit Hit;
if (Physics.Raycast(wheretoshoot.position, transform.forward, out Hit, range))
{
Instantiate(sprite, Hit.point, Quaternion.LookRotation(Hit.normal));
health = Hit.transform.GetComponent<enemyhealth>();
if(health != null)
{
health.takedamge(20);
}
}
}
currentbullet--;
if(currentbullet == 0)
{
StartCoroutine(delaybetweenshots());
}
}
IEnumerator delaybetweenshots()
{
yield return new WaitForSeconds(2f);
currentbullet = bullet;
}
}
the ui script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class switchweopen : MonoBehaviour
{
float b;
shootak47 a;
[SerializeField] Text c;
void Start()
{
a = GetComponent<shootak47>();
}
void Update()
{
b = a.currentbullet;
c.text = b.ToString();
}
}
Comment
Your answer
Follow this Question
Related Questions
scripting problem 0 Answers
Using World Space, how would I bring up info on click. 0 Answers
Slider that changes music track,Slider that controls what song is being played 1 Answer
Need help In creating android game with UI buttons ?Please Help 1 Answer
How to access Unity.UI classes in script in Unity 2019.2.1? 0 Answers