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 /
This question was closed May 07, 2018 at 08:13 AM by Myth for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Myth · Apr 06, 2012 at 09:04 PM · ontriggerenterturretontriggerexit

Turret Script Error

Hi all,

The following code is intended to print 'a' in the console and rotate a turret towards the player (or any game character, just not munitions or structures). Currently it does not. Can anyone point out what I have done wrong?

Thanks.

 #pragma strict
 // turret to turn
 var turret : Transform;
 
 // target
 var target : Transform;
 
 // angle to return to
 var stationryAngle : float = 0.0;
 
 
 function OnTriggerEnter (other : Collider) {
     if (other.isTrigger || other.gameObject.tag != 'munition' || other.gameObject.tag != 'structure')
     {
         return;
     }
     else
     {
         target = other.gameObject.transform;
     }
 }
 
 function OnTriggerExit (other : Collider) {
     if (other.isTrigger || other.gameObject.tag != 'munition' || other.gameObject.tag != 'structure')
     {
         return;
     }
     else
     {
         target = null;
     }
 }
 
 function Start () 
 {
     turret.transform.Rotate ( 0, stationryAngle, 0);
 }
 
 function Update () 
 {
     if (target != null) 
     {
         RotateTowardsPosition (target.position, 1.0);
         print('a');
     }
     else
     {
         print('b');
     }
 }
 
 function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
 {
     // Y
     // Compute relative point and get the angle towards it
     var relative = transform.InverseTransformPoint(targetPos);
     var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
     
     // add random inaccuracy
     if ( angle <  0.5 && angle > -0.5)
     {
         angle = angle + ( Random.Range(-3, 3) );
         SendMessage("Fire");
     }
     
     
     // Clamp it with the max rotation speed
     var maxRotation = rotateSpeed * Time.deltaTime;
     var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
     
         
     // Rotate
     transform.Rotate( 0, clampedAngle, 0);
     // Return the current angle
     return angle;
 }
Comment
Add comment · Show 3
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
avatar image imjay · Apr 06, 2012 at 09:36 PM 1
Share

im having a similiar issue, i want to use this method but am havin trouble understandin how it works.

{@myth ps can i use some of the code u postd?}

avatar image Myth · Apr 06, 2012 at 09:39 PM 0
Share

@imjay - that's fine

avatar image Myth · Apr 06, 2012 at 10:59 PM 0
Share

Do you have code? If you do, could you post it and I'll try and create a solution.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Apr 06, 2012 at 11:00 PM

There's a logic error in the OnTrigger events: if the target tag is "munition", it's not "structure", and vice-versa - thus the target will never be acquired. You could use a different logic:

function OnTriggerEnter (other : Collider) { if (!other.isTrigger && (other.tag == 'munition' || other.tag == 'structure')) { target = other.transform; } }

function OnTriggerExit (other : Collider) { if (!other.isTrigger && (other.tag == 'munition' || other.tag == 'structure')) { target = null; } }

This will only shoot non-trigger 'munition' or 'structure' objects. If you want to shoot anything but these two, the expression should be:

     if (!other.isTrigger && other.tag != 'munition' && other.tag != 'structure')
Comment
Add comment · Show 1 · 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
avatar image Myth · Apr 06, 2012 at 11:53 PM 0
Share

Thankyou,

avatar image
0

Answer by Myth · Apr 07, 2012 at 12:00 AM

Here is the code, with that bug (and another couple) fixed

 #pragma strict
 // turret to turn
 var turret : Transform;
 
 // target
 var target : Transform;
 
 // angle to return to
 var stationryAngle : float = 0.0;
 
 
 function OnTriggerEnter (other : Collider) {
     
     if (!other.isTrigger && other.tag != 'munition' && other.tag != 'Structure')
     {
         print ('v');
         target = other.gameObject.transform;
     }
 }
 
 function OnTriggerExit (other : Collider) {
     if (!other.isTrigger && other.tag != 'munition' && other.tag != 'Structure')
     {
         return;
     }
     else
     {
         target = null;
     }
 }
 
 function Start () 
 {
     turret.transform.Rotate ( 0, stationryAngle, 0);
 }
 
 function Update () 
 {
     if (target != null) 
     {
         RotateTowardsPosition (turret, target.position, 20.0);
     }
     else
     {
     //    print('b');
     }
 }
 
 function RotateTowardsPosition (divice : Transform, targetPos : Vector3, rotateSpeed : float) : float
 {
     // Y
     // Compute relative point and get the angle towards it
     var relative = divice.transform.InverseTransformPoint(targetPos);
     var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
     
     // add random inaccuracy
     if ( angle <  0.5 && angle > -0.5)
     {
         angle = angle + ( Random.Range(-3, 3) );
         SendMessage("Fire");
     }
     
     
     // Clamp it with the max rotation speed
     var maxRotation = rotateSpeed * Time.deltaTime;
     var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
     
         
     // Rotate
     divice.transform.Rotate( 0, clampedAngle, 0);
     // Return the current angle
     return angle;
 }
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

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Whats wrong with this ladder climb script? 1 Answer

OnTriggerExit doesn't fire when changing RigidBody (bug? feature?) 2 Answers

C# - Problem with trigger that won't activate 1 Answer

Tank Turret move on XAxis by Mouse XAxis 1 Answer

Problem using trigger to set gameobjects active 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