• 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 RetepTrun · Jun 03, 2011 at 03:27 PM · rotationpositionturret

Left/right, up/dwn of me detection

I intend my logic to be like this.
If target is right of me I rotate 2deg/frame right, otherwise I rotate left.

To do this I need to know if target is left of me or right of me.

What I have so far is the script below the ------ what happens when I use this script is my object just rotates around and around continuously

More complete info for why I am doing things this way and my goal:

I am making my own turret from scratch because it will have to be constrained within certain limits while a child of a rigidbody. Lookat and slerp use quaternions for rotations which are too hard for me to modify. More completely what I am trying to accomplish is here http://answers.unity3d.com/questions/124661/constrain-turret-rotation-while-child-of-rigidbody.html


 var target : Transform;
 var turnspeed: float = 5;
 
 function Update()
 {
     
     var targetDir = target.position - transform.position;
     var forward = transform.forward;
     
     var angle = Vector3.Angle(targetDir, forward);
     
     if (angle <= 5.0)
        // print("close");
     
     
     //var angle = 0.0;
     //var axis = Vector3.zero;
     //transform.rotation.ToAngleAxis(angle, axis);
     
     
     
     // detects if other transform is left of this object
         var right = transform.TransformDirection(Vector3.right);
         var toOther = target.position - transform.position;
         if (Vector3.Dot(right,toOther) < 0){
            // print ("other transform is left");
             //transform.Rotate(Vector3.right * Time.deltaTime);
         }
             
         var up = transform.TransformDirection(Vector3.up);
         if (Vector3.Dot(up,toOther) < 0){
           //  print ("other transform is up");
             transform.Rotate(Vector3.right * Time.deltaTime * turnspeed * -1);
         }
         if (Vector3.Dot(up,toOther) > 0){
         transform.Rotate(Vector3.right * Time.deltaTime * turnspeed * -1);
         }
 }
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

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

Answer by Joshua · Jun 03, 2011 at 04:01 PM

"If target is right of me I rotate 2deg/frame right, otherwise I rotate left."

Not a good idea. This way on a fast computer it could go an entire circle in a second, and on a slow computer it might take 20 seconds. You'll want to rotate per second, not per frame.

So, how do you see if something it to the right of.. something? You use Transform.InverseTransformPoint( ) this Transforms position from world space to local space, so where the input Vector3(1,1,1) means 1 right, up and forward from the center of the world, the output Vector3(1,1,1) would mean 1 right, up and forward from the Transform you used.

So say you have the GameObject player, you take it's transform: player.transform and you feed it the InverseTransformPoint function with the position of the enemy: player.transform.InverseTransformPoint(enemy.transform.position).

This will result in a Vector3 with the relative position of the position you fed it to the player. So Vector3(2,3,4) will mean 2 right, 3 up and 4 right of the player. So to check if it's to the right you want to check if the relative Vector3.x is larger then 0. So:

 if( player.transform.InverseTransformPoint(enemy.transform.position).x > 0 ) {

Debug.Log("the enemty is to the right of the player"); }

Comment
Add comment · Show 2 · 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 Owen-Reynolds · Jun 03, 2011 at 04:38 PM 1
Share

He wrote 2degs/second, but the actual code is moving per frame (used Time.deltaTime.). Dot products (like he's using) would probably run faster, but right now all "dotProd<0)" does is check "if angle worse than 90 degs, rotate up." So, yeah, converting to localSpace, from the turret, seems best.

avatar image RetepTrun · Jun 04, 2011 at 02:29 AM 0
Share

What Joshua says and InverseTransformPoint() sounds very promising, I'm out of time for today :( but I'll try it out tommoro

avatar image
1

Answer by RetepTrun · Jun 05, 2011 at 01:57 AM

Combining what Joshua says and the script refrence I have made the script below. It is not a complete autoturret, that requires some extra stuff "like when to fire" and "is target close enough". However this will do its best to turn towards a target and stay facing him while connected to a rigidbody but constrained to rotate only about the y axis so it works just great for a tank's turret. A con is that it quivers a little when if has found the target but that can be easily fixed in the script's logic or perhaps the turning speed.

works good for this heirarchy and nothing looks wierd.

parent rigidbody(tank body) --> turret on top(apply script here)

 var target : Transform;
 var turnspeed: float = 5;
 
 function Update()
 {
         
 var targetRelative = transform.InverseTransformPoint(target.transform.position);
 
 
     if (targetRelative.x > 0){        
         transform.Rotate(0,turnspeed * Time.deltaTime,0);
     }
     else{
         transform.Rotate(0,turnspeed * Time.deltaTime*-1,0);
     }
 }
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 Joshua · Jun 05, 2011 at 02:26 AM 0
Share

Looks right :) to stop the "quivering" when it's ai$$anonymous$$g at the target, first check if the target is more left/right then a $$anonymous$$imum value. So wrap your if {} else {} statement inside if($$anonymous$$athf.Abs(targetRelative.x) > .5) {...}

I take the absolute value to it will not turn if it's between -.5 to .5

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Turret AI script off rotation 1 Answer

Unfreezing rotation causes the position and rotation go crazy 0 Answers

Spwan and shoot bullet in direction of shooter? 2 Answers

!CompareApproximately (det, 1.0F, .005f) 2 Answers

Instantiate VS placement in editor 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