- Home /
Aim ignoring script
As i said before, i am making an Aim System and i am getting humiliated here because my knowledge about C# is poor and i can't do this in Javascript apparently.
So here is my Script:
using UnityEngine;
using System.Collections;
public class AimSys : MonoBehaviour
{
bool Click = false; //This is a trigger.
bool target = false;
public float fireRate = 0.5F;
private float nextFire = 0.0F;
public GameObject Flash;
void Update()
{
if (target = true)
nextFire -= Time.deltaTime;
if (Input.GetButton("Fire1") && nextFire <= 0)
{
Flash.SetActive(true);
Debug.Log("Target Hit!");
nextFire = fireRate;
}
}
void OnCollisionEnter(Collision collision)
{
{
if (collision.gameObject.tag == "Enemy")
target = true;
}
}
void OnCollisionExit(Collision collision)
{
{
if (collision.gameObject.tag == "Enemy")
target = false;
Flash.SetActive(false);
}
}
}
This was supposed to do the following: There is a cillinder in front of my player's weapon that is an Aim. In every frame the Aim will try to detect if there is something colliding with it. If there is something colliding with the Aim, it will search the object for the tag "Enemy". If it has the tag "Enemy", then for now a simple Log will show. Later i plan to replace the log by a small life reduce in the Enemy life bar.
The problem is that my Aim is detecting EVERYTHING as an Enemy somehow. It doesn't ignore my fire rate, so it properly shoots every two seconds if i am pressing the mouse button (Auto-Fire). I think there is something wrong with the Update Function. I can shoot everywhere, no matter if i am hitting a collider or not, and the log will be shown. It seems that the collision is being ignored but still the log is being displaced. Also the flash that i am using only disappears when i hit something else.
Any idea to how to solve this? I must be missing something basic here, but as i said i don't know much about C# yet.
Answer by doublemax · Nov 06, 2016 at 09:21 PM
if (target = true)
should be
if (target == true)
The first one is an assignment with will always evaluate to "true",
Edit: Never$$anonymous$$d, after playing 6 times somehow it fixed itself and now everything is working fine. Thanks, i didn't know that i needed to use two equals == =)
I was wrong =\
I figured out that i can STILL even if i am NOT ai$$anonymous$$g at the enemy, shoot and i will receive the "Target Hit!" log. Before editing the script, no matter how many times i shot the air, it registered. Now it only registers for the first time.
This means that if i click for the first time in anything, the log will be displayed. But the following shoots will not happen and the log will not be displayed even if i shoot the Enemy.
But if i don't shoot anything until i am ai$$anonymous$$g at the enemy and then shoot, it will work. How can i fix that?
Right now the firing does not depend on the "target" variable.
void Update()
{
nextFire -= Time.deltaTime;
if (Input.GetButton("Fire1") && target && nextFire <= 0)
{
Flash.SetActive(true);
Debug.Log("Target Hit!");
nextFire = fireRate;
}
}
Your answer
Follow this Question
Related Questions
Detect tag in C# 1 Answer
Physics.OverlapSphere, if statement, gameObject.tag 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How can i convert float to int ? 0 Answers