Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 haim96 · Sep 23, 2013 at 08:40 PM · collidertriggercoroutinelayersturret

Auto aim and shoot turret issues

Hi,

i found JS of automatic aim and shoot turret and convert it to C#.

this is what i got(with some improvement of mine to smooth the movement):

  using UnityEngine;
     using System.Collections;
     
     public class TurretController : MonoBehaviour {
     
         public Rigidbody bulletPrefab;
            private Transform target;
         private GameObject bullet;
         private float nextFire;
         private Quaternion targetPos;
     
     void OnTriggerEnter(Collider otherCollider) {
         if (otherCollider.CompareTag("airplane"))
             {
                 Debug.Log ("in");
                 target = otherCollider.transform;
                 StartCoroutine ("Fire");
             }
         }
      
     void  OnTriggerExit(Collider otherCollider) {
              if (otherCollider.CompareTag("airplane"))
             {
             Debug.Log ("out");
             target = null;
             StopCoroutine("Fire"); // aborts the currently running Fire() coroutine
             }
         }
      
     IEnumerator Fire()
     {
         while (target != null)
          {
             nextFire = Time.time + 0.5f;
             while (Time.time < nextFire)
             {
             // smooth the moving of the turret
             targetPos = Quaternion.LookRotation (target.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, targetPos, Time.deltaTime * 5);
             yield return new WaitForEndOfFrame();
             }
      // fire!
             Debug.Log ("shoot");
             bullet = Instantiate(bulletPrefab, transform.position, transform.rotation) as GameObject;
             //bullet.rigidbody.velocity = transform.forward * bulletSpeed;
         }
         }
     }


it's work fine when i place single turret on the screen. when the plane enter the collision area it trigger the aiming and get shot. problems starts when i instantiate several turrets in my scene. (they placed in line along Z axis) then they all act weird. basically they all move together get stuck on some point and shot there. the first and second turrets in line kind of work OK. i thought it because they "trigger" each other so i placed them in another layer changed layers collision settings so object in the same layer will not collide with them self. (but since i use trigger i'm not sure it effective ).

so, any advice what could be wrong or how can i implement a better turret?

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

5 Replies

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

Answer by haim96 · Sep 25, 2013 at 06:45 AM

OK, found my problem. for some reason this doesn't work well:

 targetPos = Quaternion.LookRotation (target.position);
 transform.rotation = Quaternion.Slerp(transform.rotation, targetPos, Time.deltaTime * 5);

but if i replace it back with Transform.lookat(target) it works fine. not exactly as i want but much better.

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 haim96 · Sep 25, 2013 at 12:31 PM 1
Share

O$$anonymous$$, fixed that too with this code:

 Vector3 dir = target - transform.position;
 Quaternion targetRotation =Quaternion.LookRotation(dir);
 transform.rotation =Quaternion.RotateTowards(transform.rotation, targetRotation,Time.deltaTime * speed);
avatar image
0

Answer by mikerock12 · Oct 17, 2019 at 11:48 AM

thanks for sharing it i found it so amazing Youtube Go glad to be here

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 farmvillekings · Feb 23, 2021 at 03:45 PM

Thanks, it works for me too. but I also have to replace transform.lookat. iMegaCam for Pc. However, thanks again.

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 geekspy8 · Mar 05 at 03:49 PM

I'm glad somebody's still talking about these subjects. Even I'm unable to discuss such issues on my technology blog @WikiesforPC

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 hraza886 · Apr 24 at 08:13 PM

hard to understand such topics but good work keep it up. Reviews About New Apps And Games Click Here

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

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

Related Questions

Using two trigger colliders 1 Answer

Trigger Collider not detecting game objects? 0 Answers

After building move doesnt move 1 Answer

Pseudo code: trigger portal system with coroutines 0 Answers

Child object's collider (on a different layer) is interfering with parent Physics... 0 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