Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Nov 19, 2012 at 12:32 AM by Myth for the following reason:

Other

avatar image
0
Question by Myth · Jul 31, 2011 at 06:14 AM · turrettargetingfiring

My turret is a pacifist.

I have the following code - and all the turret does is look at me.

first script - should control the elevation and fire the gun - not sure it is actually doing anything.

 var shootAngleDistance = 10.0;
 var target : Transform = null;
 var rotateSpeed : float = 20.0;
 var count : int = 0;
 var bullitPrefab : Transform;
 
 
 function Update () 
 {
     count = count - 1;
     if (count < 0)
     {
         target = null;
         count = 30;
     }
     
     
     if (target == null)
     {
         return;
     }
     else
     {
         RotateTowardsPosition(target.position, rotateSpeed);
              print ("yay");
     }
     
     // If we are almost rotated towards target - fire one clip of ammo
     var forward = transform.TransformDirection(Vector3.forward);
     var targetDir = target.position - transform.position;
     if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
     {
         var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position, Quaternion.identity);
     
         bullit.gameObject.tag = "enemyProjectile";
         bullit.rigidbody.AddForce(transform.forward * 1500);
 
     }
 }
 
 
 
 function targetAim (aquiredTarget : Transform)
 {
      target = aquiredTarget;
 }
 
 
 function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
 {
     // Compute relative point and get the angle towards it
     var relative = transform.InverseTransformPoint(targetPos);
     var angle = Mathf.Atan2 (relative.y, relative.z) * Mathf.Rad2Deg;
     // Clamp it with the max rotation speed
     var maxRotation = rotateSpeed * Time.deltaTime;
     var clampedAngle = Mathf.Clamp(angle, -maxRotation, maxRotation);
     // Rotate
     transform.Rotate(clampedAngle, 0, 0);
     // Return the current angle
     return angle;
 }



second script - triggered by another with a collision event, of which the transform is sent to this one. This script works - as far as I am aware!

 var shootAngleDistance = 10.0;
 var target : Transform = null;
 var rotateSpeed : float = 20.0;
 var count : int = 0;
 
 
 function Update () 
 {
     count = count - 1;
     if (count < 0)
     {
         target = null;
         count = 30;
     }
     
     
     if (target == null)
     {
         return;
         //transform.Rotate(Vector3.up * (Time.deltaTime * 5));
     }
     else
     {
         // Rotate towards target
         /*
         var targetPoint = target.position;
         var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
         */
         RotateTowardsPosition(target.position, rotateSpeed);
 
     }
     
     // If we are almost rotated towards target - fire one clip of ammo
     var forward = transform.TransformDirection(Vector3.forward);
     var targetDir = target.position - transform.position;
     if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
     {
         BroadcastMessage("targetAim",target.transform);
     }
 }
 
 
 
 function targetSighted (aquiredTarget : Transform)
 {
      target = aquiredTarget;
 }
 
 
 function RotateTowardsPosition (targetPos : Vector3, rotateSpeed : float) : float
 {
     // Compute relative point and get the angle towards it
     var relative = transform.InverseTransformPoint(targetPos);
     var angle = Mathf.Atan2 (relative.x, relative.z) * Mathf.Rad2Deg;
     // 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;
 }
 
 
 /*
 
 function CanSeeTarget () : boolean
 {
     if (Vector3.Distance(transform.position, target.position) > attackRange)
     {
         return false;
     }
         
     var hit : RaycastHit;
     
     if (!Physics.Linecast (Vector3(transform.position.x,transform.position.y + 0.5,transform.position.z), target.position,2)) {
         return true;
     }
 
     //return false;
 }
 
 */



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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Myth · Aug 01, 2011 at 03:12 AM

 // If we are almost rotated towards target - fire one clip of ammo
     var forward = transform.TransformDirection(Vector3.forward);
     var targetDir = target.position - transform.position;
     if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
     {
         var bullit = Instantiate(bullitPrefab ,transform.Find("spawnPoint").transform.position, Quaternion.identity);
 
         bullit.gameObject.tag = "enemyProjectile";
         bullit.rigidbody.AddForce(transform.forward * 1500);
 
     }

I have identified this as the root of the problem, when the if statement is removed the script works. What I need, is this script to test if the angle is close to /pointing at the the target.

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
avatar image
0

Answer by Bunny83 · Jul 31, 2011 at 09:27 AM

That's a weired script. What's the count good for? I guess that's one of your main problems.

  • You count backwards each frame which is kinda crazy to do something framebased.
  • The first frame count is 0. You subtract 1 and it becomes -1. Right below you null your target and "reset" your counter to 30. In the next line you leave to whole Update function because the target is null.
  • Your 30 frames counter will null your target every 30 frames and since you don't set/reset the counter somewhere else it happens EVERY 30 frames.
  • The second script do the same crazy things and in addition you have two functions (targetSighted, CanSeeTarget ) that seems to be important for the whole process but in your code you don't call them anywhere. I guess you don't posted this part.

I'm pretty sure that your two target-nulling-counters will break your whole script(s). It would be essential how you actually get a target.

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 · Jul 31, 2011 at 10:28 PM 0
Share

At this point the count is required to reset the targeting, this it does. If you can suggest a better way to do this, I would greatly appreciate it.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do i predict the position of my player for the turret to shoot at? 2 Answers

Locking on target with angle (mostly working) 1 Answer

Turret Firing problem [C#] 2 Answers

Tower Defense turret with multiple targets within range. Help with targeting and nulling. 1 Answer

How to stop turret firing? 2 Answers


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