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 BillyM148 · Jan 14, 2014 at 09:05 AM · rotationrotatelookatlookrotation

Rotation using Unity2D

I have a game object at the top middle of my screen. I have no idea how to be able to rotate it so it will follow my mouse pointer or even touch position. It doesn't matter which, I'm currently building it follow my mouse position so let's stick with coding it "LookAt" my mouse pointer.

If you don't understand what I want to be able to do, let's take a look at this simple image. alt text

The game object (a 2D sprite) should be able to rotate back and forth to point at an x and y coordinate where my mouse pointer is. Every time I tried to use LookRotation or LookAt, I just got weird results that always rotated it incorrect ways. Can somebody help me along here in getting this up and going?

P.S. Preferably in Javascript.

Thanks a ton!

rotation.png (13.1 kB)
Comment
Add comment · Show 2
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 robertbu · Jan 14, 2014 at 09:08 AM 0
Share

Is this a 3D game or a 2D game? What code have you tried that did not work out? When the rotation of the object (0,0,0), what side of the object is the 'forward'...that is the side you want to point at the mouse?

avatar image BillyM148 · Jan 14, 2014 at 10:28 AM 0
Share

2D Game, hence the subject name "Rotation using Unity2D"

Here's my LookAt script that also fires a bullet. I edited this code from an example.

 var dir : Vector3;
 private var tempVector : Vector3;                //We need this to figure out where to look
 private var tempVector2 : Vector3;                //We need this to figure out where to look
 private var lookDir : Vector3;                    //The look direction
 public var bullet : GameObject;                    //Bullet prefab
 
 
 public var fireTime : float;                    //Fire time
 private var tmpFireTime : float;                //Tmp fire time
 public var spawnBulletPosition : Transform;        //Bullet Spawn position
 
 function Start () 
 {
 
 }
 
 function Update ()
 {
     //If the game is not running on a android device
     if (Application.platform != RuntimePlatform.Android)
     {
         //Horizontal axis and Vertical axis is not 0
         if (Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
         {
             //Set dir x to Horizontal and dir z to Vertical
             dir = new Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"), -10);
         }
         else
         {
             //Set dir to 0
             dir = new Vector3(0,0,0);
         }
         //Find the center of the screen
         tempVector2 = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f);
         //Get mouse position
         tempVector = Input.mousePosition;
         //Set tempVector z to tempVector.y
         ///tempVector.z = tempVector.y;
         //Set tempVector y to 0
         //tempVector.y = 0;
         //Set lookDir to  tempVector - tempVector2
         lookDir = tempVector - tempVector2;
         
         //If left mouse click
         if (Input.Get$$anonymous$$ouseButton(0))
         {
             //Check to see if we can fire yet.
             if (tmpFireTime >= fireTime)
             {
                 //Set tmpFireTime to 0
                 tmpFireTime = 0;
                 //Instantiate bullet                                                        
                 var go = Instantiate(bullet, spawnBulletPosition.position, Quaternion.LookRotation(lookDir)) as GameObject;
                 go.rigidbody2D.AddForce(go.transform.forward * -700);
             }
             //Couldn't fire yet
             else if (tmpFireTime < fireTime)
             {
                 //Add 1 to tmpFireTime
                 tmpFireTime += 1 * Time.deltaTime;
             }
         }
     }
     
     //Rotate Turret
     transform.rotation = Quaternion.LookRotation(lookDir);    
 }



This code cause the game object to rotate and tilt in incorrect directions ins$$anonymous$$d of how I want it to in the example image.

See how it tilts incorrectly? alt text

1.png (135.3 kB)

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kUr4m4 · Jan 14, 2014 at 10:29 AM

are you looking for Transform.LookAt maybe?

http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html

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 BillyM148 · Jan 14, 2014 at 10:39 AM

I don't know enough to know that or not. But i did change

this line -

transform.rotation = Quaternion.LookRotation(lookDir);

to this -

transform.LookAt(lookDir);

and i still have the same problem with it.

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 Bunny83 · Jan 14, 2014 at 11:08 AM 0
Share

@Billy$$anonymous$$148: LookRotation takes a direction whereas LookAt takes a position. If you don't know the difference you should inform yourself about that. Also you shouldn't post such annotations as answer. Post a comment ins$$anonymous$$d. An answer should answer the question. UA is not a forum, it's a Q&A site

avatar image
0

Answer by Bunny83 · Jan 14, 2014 at 11:03 AM

I never used the new 2d features since I stick with unity 4.0 at the moment. However most people forget that LookAt or LookRotation takes two parameters. If you don't pass a second it defaults to Vector3.up which is just a constant for (0, 1, 0).

The second parameter controls the rotation around the look-at-axis.

LookAt makes the object's forward axis (z, the blue one) to point directly at the target position (the first parameter). The second parameter just rotates the object around the forward axis so the object's up axis (y, the green one) points most closely to the given up-vector in the second parameter.

Depending on where "up" is on your object you might want to pass a different up-vector.

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

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

Related Questions

How can I rotate my player head to face a target but only when the target is in the front of the player ? 1 Answer

LookAt inaccurate rotation issue (javascript) 1 Answer

Lock rotation axis? 4 Answers

LookAt script locks Z axis when parent is rotated 0 Answers

How to let navMeshAgent smoothly aim/rotate smoothly to two different GameObjects,How to let navMeshAgent smoothly aim to two different GameObjects 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