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 skoandi · Apr 07, 2013 at 01:14 PM · rotationquaterniondirectionlookateulerangles

LookAt on only 1 axis for 2D games?

http://www.officegamespot.com/officegames/appleshooter.htm

Look at this game, the bow rotates based on where the mouse is

How can I make it? I've been trying for 5H (seriously!) If you know quaternions/eulers please help me

Here's my project (.unitypackage 0.5mb) http://www17.zippyshare.com/v/85393294/file.html

I've came across this site: http://unity3dtutorial.com/unity-3d-tutorials/dealing-with-directions-movement-in-unity-3/ Maybe someone will find it useful, however I still can't solve this problem

        var mPoss = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
     transform.position.x = mPoss.x;
     transform.position.y = mPoss.y;
     var r = (transform.position - center.transform.position).normalized;
     var t = Quaternion.LookRotation(r);
     transform.eulerAngles = Vector3(t.x,t.y,t.z);

Here's another try

 function Update () {
     var MouseWorldPosition = Camera.mainCamera.camera.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0));
     MouseWorldPosition.z = 0;
     //find the target rotation quaternion
     lookRot = Quaternion.LookRotation((MouseWorldPosition - transform.position),Vector3.up);
     lookRot.y = 90;
     lookRot.z = 270;
     //rotate towards the mouse
     transform.rotation = lookRot;
 }
 /*    mPoss = Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
     obj.transform.position = Vector3(mPoss.x,mPoss.y,1.854246);
     lookDir = Quaternion.LookRotation(obj.transform.position,transform.position).eulerAngles;
     //lookDir.y = 90;
     //lookDir.z = 270;
     lookDir.x = transform.eulerAngles.x;
     transform.rotation = Quaternion.Slerp( transform.rotation, Quaternion.Euler(lookDir),Time.deltaTime*10);
 //    lookDir = (obj.transform.position - transform.position);
 //    lookRot = Quaternion.LookRotation(lookDir);
 //    transform.eulerAngles = Vector3(lookRot.x,lookRot.y,lookRot.z);
 //    transform.RotateAround(transform.position, Vector3.forward, lookRot.z);
 //    transform.LookAt(Vector3(transform.position.x,obj.transform.position.y,obj.transform.position.z));
 //    transform.eulerAngles.z = 270;
     //transform.eulerAngles.x += 90;
 //    transform.eulerAngles.y = 90;*/
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
0

Answer by Rynold · Jan 29, 2014 at 05:12 AM

Here's Code I wrote in order to get my cannon to rotate towards where my player clicks on the screen. It will only fire once it reaches its designated angle. Fully commented Hope it helps you out.

 if (Input.GetMouseButtonDown(0))               //If the mouse was pressed.
             {
                 centerTarget = new Vector2(0.0f, 7.3f);                                                                                                  //Sets center Target to always be straight up in the middle
                 targetVector = new Vector2(mousePosition.x, mousePosition.y) - new Vector2(transform.position.x, transform.position.y);                  //Ses the target to be where the mouse is on the screen     
                 newAngle = Mathf.Acos(Vector2.Dot(targetVector, centerTarget) / (targetVector.magnitude * centerTarget.magnitude)) * 180 / Mathf.PI;     //Sets the new angle value to be the angle between the center and the new target
                 if (centerTarget.x < targetVector.x)                                                                                                     //If the new target is to the right of the center...
                     newAngle = -newAngle;                                                                                                                //make the angle negative
                 targetAngle = Mathf.Round(newAngle);                                                                                                     //Rounds the new angle to the nearest unit.
                 targetRotation.eulerAngles = new Vector3(0, 0, targetAngle);                                                                             //Sets the Target quaternion's euler to a vector with X and Y at 0 and Z at the new angle.
                 targetObjLocation = Camera.main.ScreenToWorldPoint(new Vector3(e.mousePosition.x, Screen.height - e.mousePosition.y, 7));                //Sets the target objects location to be where the mouse is.
                 isRotating = true;                                                                                                                       //Sets isRotating to true to tell the rest of the code its rotating
                 Instantiate(targetObj, targetObjLocation, Quaternion.Euler(new Vector3(0, 0, 0)));                                                       //Instantiates the target object.
             }
             if (isRotating == true) //If the object is rotating
             {
                 if (transform.rotation != targetRotation)          //If it hasn't rotated all the way to where its supposed to yet
                 {
                     float step = rotatinSpeed * Time.deltaTime;                                                  //Set a step float to the rotating speed * Time.deltaTime
                     transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, step);     //Rotate gradually towards the new angle
                 }
                 else                                              //If it has rotated all the way to where its supposed to
                 {
                     if(coolDown <= 0)
                         Fire();                                   //call the fire function
                     isRotating = false;                           //Tell the system its not rotating anymore
                 }
 
             }
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

13 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

Related Questions

Quaternion Equivalent of restricting an axes rotation to zero 3 Answers

Is there no way to get reliable angles from a rigidbody? 2 Answers

Look at like rotating around y axis 1 Answer

ConfigurableJoint - angular positions (rotation) problem 1 Answer

EulerAngles conversion Quaternion problem 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