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 AlexClarke117 · Apr 15, 2014 at 06:57 PM · rotationtransformeuleranglessimple

Simple object rotation & LookAt

Hi, I am trying to simply rotate an object along the X axis while also getting the object to look at my mouse position on it's Y axis, I can get each of these to work on there own f I comment one of the parts out but if I leave both in, the rotation doesn't happen and only the LookAt function will continue to work. Hopefully you can see what I am trying to do? So they may be a better way to do this.

Here is my code sample:

 using UnityEngine;
 using System.Collections;
 
 public class movementControler : MonoBehaviour 
 {
     public GUIText testData;
     public GameObject sphere;
     float speed = 0.15f;
 
     private float mouseX, mouseY;
     private Vector3 worldPos;
     private float cameraDif;
     float xRot;
 
     // Use this for initialization
     void Start () 
     {
         cameraDif = camera.transform.position.y - sphere.transform.position.y;
         xRot = sphere.transform.localRotation.x;
     }
     
     // Update is called once per frame
     void Update () 
     {
         mouseX = Input.mousePosition.x;
         mouseY = Input.mousePosition.y;
 
         worldPos = camera.ScreenToWorldPoint(new Vector3(mouseX,mouseY,cameraDif));
 
         if(Input.GetMouseButton(0))
         {
             //When using both, only LookAt works, but using them by themselves works how I want.
             sphere.transform.eulerAngles = new Vector3(-xRot++,0,0);
             sphere.transform.LookAt(worldPos);
         }
     }
 }
 
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 · Apr 15, 2014 at 07:03 PM 0
Share

There are some issues here. First localRotation is a Quaternion...a 4D construct where the x,y,z, and w components don't represent angles. Second, you need to describe the exact behavior you want in combining these two rotations. As you found out, LooAt() sets all three axes, so fixing the issue will depend on getting a very clear description of your desired behavior.

avatar image AlexClarke117 · Apr 15, 2014 at 07:07 PM 0
Share

All I want to achieve is a constant rotation along the objects local x axis while having the y axis follow the location of the mouse.

Should I be adding the rotational value to the xAxis that I want before applying the LookAt?

1 Reply

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

Answer by robertbu · Apr 15, 2014 at 07:48 PM

The way you have things structured here makes writing an answer a bit more difficult. My suggestion is that you have two game objects for your sphere, 1) an empty game object as a parent, and 2) the physical object as the child. 'sphere' would refer to the empty game object, and 'sphereChild' the physical game object. You would add the following at line 29:

 worldPos.y = sphere.transform.position.y;

This change will result in the parent (empty) game object only rotating on the 'y' axis.

The physical object (child) would then have this happen:

 xRot += speed * Time.deltaTime;
 sphereChild.transform.localEulerAngles = new Vector3(xRot,0,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 AlexClarke117 · Apr 15, 2014 at 08:13 PM 0
Share

This works great thank's. Now my object gains a constant rotation along the xAxis while constantly tracking the mouse on it's yAxis.

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

Problems caused by non-unique Euler angle solutions 1 Answer

Set a relative (custom) speed to a rotation 1 Answer

Object wont rotate in the opposite direction 1 Answer

How do you smoothly transition/Lerp into a new rotation? 3 Answers

Rotation drift with transform.eulerAngles 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