- Home /
How to make it so only my player activates Trigger
Ok so I made an AI for a zombie like thing that follows you (currently a cube for testing) and I wrote this script:
var target : Transform;
var enemy : Transform;
var moveSpeed : float = 5.0;
var inRange : boolean = false;
function Start() {
moveSpeed = moveSpeed*Time.deltaTime;
}
function OnTriggerEnter() {
inRange = true;
}
function OnTriggerExit() {
inRange = false;
}
function Update() {
if(inRange == true) {
enemy.transform.LookAt(target);
enemy.transform.position = Vector3.Lerp(
enemy.position, target.position,
Time.deltaTime * moveSpeed);
}
}
So the cube follows me but whenever I shoot my gun it just freezes the cube in place. I supposed this was caused by the bullet (currently an invisible sphere) glitching the trigger. Can someone help me find out whats wrong please?
PS this is the gun script:
var bulletPrefab : Transform;
var shootSpeed : float = 99999999;
var clipAmmo = 12;
var Ammo = 196;
var ammoLeft : boolean = true;
var gunShotSFX : AudioSource;
var noAmmoSFX : AudioSource;
var reloadSFX : AudioSource;
function Update() {
if(clipAmmo > 0) {
if(Input.GetButtonUp("Fire1")) {
var bullet = Instantiate(bulletPrefab,
GameObject.FindWithTag("BarrelFront").transform.position,
GameObject.FindWithTag("Gun1").transform.rotation);
clipAmmo = clipAmmo - 1;
gunShotSFX.Play();
}
}
if(ammoLeft == true) {
if(Input.GetKeyUp("r")) {
if(clipAmmo == 0) {
clipAmmo = 12;
Ammo = Ammo - 12;
reloadSFX.Play();
}
if(clipAmmo == 1) {
clipAmmo = 12;
Ammo = Ammo - 11;
reloadSFX.Play();
}
else if(clipAmmo == 2) {
clipAmmo = 12;
Ammo = Ammo - 10;
reloadSFX.Play();
}
else if(clipAmmo == 3) {
clipAmmo = 12;
Ammo = Ammo - 9;
reloadSFX.Play();
}
else if(clipAmmo == 4) {
clipAmmo = 12;
Ammo = Ammo - 8;
reloadSFX.Play();
}
else if(clipAmmo == 5) {
clipAmmo = 12;
Ammo = Ammo - 7;
reloadSFX.Play();
}
else if(clipAmmo == 6) {
clipAmmo = 12;
Ammo = Ammo - 6;
reloadSFX.Play();
}
else if(clipAmmo == 7) {
clipAmmo = 12;
Ammo = Ammo - 5;
reloadSFX.Play();
}
else if(clipAmmo == 8) {
clipAmmo = 12;
Ammo = Ammo - 4;
reloadSFX.Play();
}
else if(clipAmmo == 9) {
clipAmmo = 12;
Ammo = Ammo - 3;
reloadSFX.Play();
}
else if(clipAmmo == 10) {
clipAmmo = 12;
Ammo = Ammo - 2;
reloadSFX.Play();
}
else if(clipAmmo == 11) {
clipAmmo = 12;
Ammo = Ammo - 1;
reloadSFX.Play();
}
}
}
if(0 >= Ammo) {
ammoLeft = false;
}
else if(Ammo > 0) {
ammoLeft = true;
}
if(clipAmmo < 1) {
if(Input.GetButtonUp("Fire1")) {
noAmmoSFX.Play();
}
}
}
function OnGUI() {
GUI.Box(Rect (10,10,90,25), "Clip: " + clipAmmo);
GUI.Box(Rect (10,35,90,20), "Ammo: " + Ammo);
}
Answer by TeddyDief · Oct 12, 2012 at 06:02 PM
To fix this, and to optimize, I'd suggest addressing this by creating two new Layers:
1) Player - assigned only to the player 2) PlayerTrigger - assigned to triggers you want only the player to activate.
Then go into your Project Settings > Physics, and set up the collisions so that Player still collides with whatever you need, but PlayerTrigger collides ONLY with the Player layer.
If you do this, OnTriggerEnter / OnTriggerExit will only be called if the player enters the trigger!
Your answer
Follow this Question
Related Questions
How to add gun to the model, as when it animate, the gun is dis-positioned. 2 Answers
player collision detectionn registering problem with an object 1 Answer
moving gun in first person shooter while walking 8 Answers
FPS TUTORIAL: AI robot not taking damage. 0 Answers
Network fps swap cameras 1 Answer