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
0
Question by Karsnen_2 · Dec 03, 2011 at 12:02 AM · rotationscriptingbasicsanglelookat

Look at an angle

Hello,

I have an angle var and a turret. I would like the turret to look at that angle which is an input from the user.

GameObject :

I have a turret model which is rigged and capable of rotating. I have an angle in another script which is static and is used in another script attached to the turret. The angle is varied using a sidescroller GUI.

Code :

 var lockPos =0;
 
 function Update ()
 {
 
     transform.LookAt(Vector3(Projectile.angle,lockPos,lockPos), Vector3.right);
 }  


Code Explanation:

I am trying the turret to look at angle which is "Projectile.angle" along the x-axis. I have defined Vector3.right as it is the x-axis(world). Moreover I made an attempt to lock the rotation in the y and z axis by defining it at zero.

Problem:

The turret for some reason rotates to the Z axis and lifts it's cannon hand upwards, with respect to the x axis. Beyond that I even tried to adjust the "angle" which had no effect on the angle of the cannon.

Request:

I humbly request for the shot on the problem on the code or the code I should change. I am just trying to create Projectile motion in a 2D plane(x-y plane) using the LookAt() function. Apart from this, (not a main concern) if you guys know any good turret/cannon models to import - please do let me know.

Thank YOU

125SCREENSHOT125

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

1 Reply

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

Answer by aldonaletto · Dec 03, 2011 at 01:38 AM

That's not the way Transform.LookAt works - it can look at some transform or world point, not to an specific direction or angle. If you want to aim to some angle, you can create a vector with this elevation angle and add it to transform.position - this will create a world point in the desired direction, to which you can aim using LookAt:

function Update (){
    var angle = Projectile.angle * Mathf.Deg2Rad; // convert the angle to rads
    // create a vector with the desired angle:
    var targetDir = Vector3(0, Mathf.Sin(angle), Mathf.Cos(angle));
    // make transform look at a point in the desired direction:
    transform.LookAt(transform.position + targetDir);
}
Notice that this code will generate an angle that rotates around X, what means in the YZ plane. Something is telling me that what you really want is an elevation angle from the X axis - supposing Y is the vertical direction, what is needed in this case is an angle around Z, or in the XY plane. If this is true, the targetDir calculation should be the following:
 var targetDir = Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0);

This will generate a vector with an elevation of Projectile.angle degrees from the X axis, supposing Y is the vertical direction.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Make object only rotate in 90 degree increments 0 Answers

How to clamp Lookat? 2 Answers

Problem with Z rotation when rotating X and Y and the same time. 2 Answers

Getting the rotation of an object does not return correct rotation 1 Answer

Rotation Reset 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