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
2
Question by D_e_l_t_a · Dec 30, 2011 at 08:32 PM · rotatelookatsnap

Look At objects using a SNAPPED angle

I'm wondering how I can make a gun turret point at an enemy but only at 90 deg angles and not constantly but every 2 seconds. The 2 second part will be Invoked but on every 2 seconds I want the gun to 'snap' to the enemy direction using only 90 degree snapped turns, like North,Nth East, East, South, Sth East etc, and nothing inbetween. Had a quick attempt using a stand-in Cube with Transform.LookAt but the snapping has me stumped. Any suggestions ?

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 Owen-Reynolds · Dec 31, 2011 at 01:27 AM 0
Share

Will the gun ONLY aim 45, 90, 135 ... and never in-between. Or will it snap towards the correct angle, but at most 45 degrees (so any angle < 45 will snap next tick)?

avatar image D_e_l_t_a · Dec 31, 2011 at 03:40 PM 0
Share

Yes, never inbetween, but at 90 deg increments. All are 3 hour increments of a clocks hand, if you like, from 12.00 o'clock through to 3.00, through to 6.00 etc.

avatar image D_e_l_t_a · Dec 31, 2011 at 03:42 PM 0
Share

Sorry, 45 deg angle increments! So scrap the clock analogy ...North, Nth East, East, Sth East etc,,

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Dec 31, 2011 at 08:02 PM

There are three parts. The easier is, if you have an angle, how do you aim that way:

 float A; // some angle we figured out, happens to be 0,45,90...
 transform.rotation = Quaternion.Euler(0,A,0);

A harder one is how to figure out the 0-360 angle to your target. One obvious way is with 8 ifs, one for each 45-degree section. The problem is, you can tell between 0 and 90 degrees by whether x or z is larger. Between 0 and 45 degrees, you need to check the tangent to see which side of 22.5 degrees you are on. Ick.

Unity stuff like Quaternion.Angle() gives the "off by" angle -- it can't tell 45 degrees left from 45 degrees right. You could fix that, but may as well use real trig -- arc-tangent. That gives the angle, but in radians, aiming 0 degrees right and going clockwise. so, something like:

 // direction to target:
 Vector3 D = target.position - transform.position;
 float A = Mathf.Atan2(D.z, D.x);
 // Now convert A to clockwise, degrees, with 0 north:
 A = (A*-1) * 180.0f/(Mathf.PI) + 90;

The last part is rounding to the nearest 45. The standard way to round anything into groups is to divide by size, round normally, then expand back out:

 float Arounded = Mathf.Round((A/45.0f)*45;

For example, you have 112 degrees: (112/45)=2.48; rounded=2; *45 is 90.

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 adrenak · Dec 31, 2011 at 08:13 PM

You cant have exact multiple of 45 degree angles cause the enemy character would never be at exact multiple of 45 degress (say 43, or 47). So you need to have 45 plus/minus deltaDegress (lets say deltaDegrees = 3). So the gun will respond if the target is between angles 42 and 48, 87 and 93, 132 and 138 , 177 and 183 and so on. I would solve this problem using basic trigonometry ( never missed math classes ). I'll assume your enemy is tagged ' target'.

In the update function, find the local angle between the gun and the target with respect to the gun, assuming the angle variable is 'angle'

 if(((Mathf.Abs(Mathf.sin(angle)) > 0.67 ) && (Mathf.Abs(Mathf.sin(angle)) < 0.743 )) ||  ((Mathf.Abs(Mathf.sin(angle)) > 0.82 ) && (Mathf.Abs(Mathf.sin(angle)) < 0.95 ) ){
 
     //then rotate the gun directly to the target. If a raycast from the gun in the forward 
     //direction hits an object with tag 'target' then fire
        
 
 }

Apart from this u may want that the gun goes back to its initial rotation again. For this you can record the rotation of the gun in the Start function as soon as the gun is initialised and when the target does not make the required angles with the gun, slerp it back to the initial rotation.

Disclaimer : I am better at conceptualizing so I sorry if syntax is wrong, specially the circular bracket in the if statement, I tend to make mistakes with them, Other wise I have used this kind of code before. There might be better ways of doing it :)

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

6 People are following this question.

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

Related Questions

How can I make an object always look at the mouse? 2 Answers

How to prevent Z axis rotation ? 0 Answers

making and object rotate with mouse 3 Answers

move to a position not being looked at 1 Answer

turning on x and y axis for turret but not using lookat? 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