- Home /
i fixed it
my enemy is broken
so i have an enemy with this code:
using UnityEngine;
using System.Collections;
public class enemy : MonoBehaviour {
[SerializeField]
private float CrabSpeed;
[SerializeField]
private GameObject projectile;
[SerializeField]
private Collider2D player;
[SerializeField]
private LookForShootables lfs;
[SerializeField]
private Collider2D radar;
Quaternion zrotate;
bool lookingright;
bool SeePlayer;
bool SeePlayerShootable;
bool Triggered;
private float TTN;
[SerializeField]
private float MTTN;
private float reloadtime = 0;
// Use this for initialization
void Shoot(float mrt)
{
if (lookingright == true)
zrotate = new Quaternion(0, 0, 180, 0);
if (lookingright == false)
zrotate = new Quaternion(0, 0, 0, 0);
Rigidbody bullet;
bullet = Instantiate(projectile, transform.position, zrotate) as Rigidbody;
bullet.AddForce(20, 0, 0);
reloadtime = reloadtime + mrt;
}
void TimeToNeutral()
{
if (TTN != 0 && Triggered == true)
TTN = TTN - 1;
if (TTN == 0)
{
Triggered = false;
TTN = MTTN;
}
}
void OnTriggerEnter2D(Collider col)
{
if (col.tag == "Player")
{
Debug.Log("holy crap!");
SeePlayer = true;
}
}
void FixedUpdate()
{
SeePlayerShootable = lfs.shootables;
//--------------------------------------
if (lookingright == !false)
transform.localScale = new Vector3(-3, 3, 1);
if (lookingright == !true)
transform.localScale = new Vector3(3, 3, 1);
//thesearedebugs
if (Input.GetKey(KeyCode.H))
lookingright = true;
if (Input.GetKey(KeyCode.G))
lookingright = false;
if (reloadtime > 0)
reloadtime = reloadtime - 1;
//*****************************************************************
//-----------------------------------------------------------------
//*****************************************************************
if (SeePlayer == true)
{
Triggered = true;
Debug.Log("triggered!!!");
}
if (SeePlayerShootable == true && reloadtime == 0)
{
Shoot(60);
Debug.Log("shooting");
}
if (SeePlayer == false)
TimeToNeutral();
}
}
and another object has this:
using UnityEngine;
using System.Collections;
public class LookForShootables : MonoBehaviour
{
[SerializeField]
private Collider2D player;
public bool shootables;
void OnTriggerEnter2D(Collider col)
{
if (col.tag == "Player")
shootables = true;
}
}
the game is in 2d and the problem may lie with the triggers. what am i doing incorrectly? (i've only started coding so i probably did many things incorrectly.)
You need to explain what's broken. What do you do, what do you expect to happen and what happens ins$$anonymous$$d?
Answer by m1le · Oct 13, 2016 at 10:55 AM
Like doublemax said, explain your problem. And i suggest you to change your if statement : if true if false, by if and else.
Follow this Question
Related Questions
Collider other doesnt recognize other gameobjects 1 Answer
Boss Health Bar 1 Answer
Enemy's health wont get set from different script. 1 Answer
What is the difference between these two scripts? 1 Answer
Collision Detection - Strange issues! Working when it shouldnt, not working when it should! 2 Answers