Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrLolEthan · Oct 12, 2012 at 05:00 PM · triggerplayerfpsgun

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);
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges