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
1
Question by alemimi · Dec 14, 2013 at 11:21 AM · transform.rotationnotworkingmaking

Making the 'transform.rotation' of an object the 'transform.rotation' of another object

Now, the situation I'm trying to solve is a bit more complicated than what the title says;

In my scene, I have a Sphere (which is supposed to be your 'companion', it aims where you aim and moves where you move), the problem here is that the First Person Controller only rotates on the Y axis, while the Main Camera of the First Person Controller rotates only on the X axis when you aim with the Mouse. In my scene there's a button too, which 'activates' the sphere, which starts to follow you and aim where you do.

To make this work, I put the Sphere inside an EmptyGameObject that copies the position of the Player, and I put the Sphere a bit far from it, so that when it follows the Player the Sphere is not inside it, I wrote a Script for it, and it works, infact the position is not a problem, now the problem is the rotation of the Sphere itself, infact I don't want the EmptyGameObject to rotate, i want the Sphere itself to rotate when the Player does (I have two Cubes attached to the Sphere so that I can see if the Sphere is actually rotating);

I wrote this code:

 using UnityEngine;
 using System.Collections;
 
 public class SphereGO : MonoBehaviour {
 
     public bool isSphereGameObject;
     public bool isSphereEmptyGameObject;
     public GameObject yRotationObject;
     public GameObject xRotationObject;
     public static bool sphereFollowPlayer;
     public GameObject followObject;
 
     // Use this for initialization
     void Start () {
         sphereGoFollow = false;
     }
     
     // Update is called once per frame
     void Update () {
         if(isSphereEmptyGameObject){
             if(sphereFollowPlayer == true){
                 transform.position = Vector3.Lerp (this.transform.position, followObject.transform.position, 0.1f);
             }
         }
         if(isSphereGameObject){
             if(sphereFollowPlayer == true){
                 transform.localPosition = Vector3.Lerp (this.transform.localPosition, new Vector3(1.169f,1.582f,1.12f), 0.1f);
                 transform.rotation = Quaternion.Euler(xRotationObject.transform.rotation.x, yRotationObject.transform.rotation.y, transform.rotation.z);
             }
         }
     }
 }
 

All the code that is written inside the 'isSphereEmptyGameObject' works for the EmptyGameObject, and all the code written inside the 'isSphereGameObject' works for the Sphere itself, so I didn't need to create two separate scripts for the EmptyGameObject and the Sphere.

Now the main problem, the Sphere doesn't rotate, or better perhaps, the Sphere rotates a TINY bit when I rotate my view. What's the problem?

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 KellyThomas · Dec 14, 2013 at 12:13 PM

Transform.rotation is a Quaternion, but you are taking it's properties and treating them as Euler angles

Comment
Add comment · Show 5 · 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 alemimi · Dec 14, 2013 at 12:31 PM 0
Share

Yeah, I knew there was something wrong with Quaternion.Euler, but how can I solve my problem? What should I write ins$$anonymous$$d?

avatar image KellyThomas · Dec 14, 2013 at 12:40 PM 0
Share

Quaternion.eulerAngles is the euler angle representation... so you can use Transform.rotation.eulerAngles to access x,y,z euler angles from a tranform.

avatar image alemimi · Dec 14, 2013 at 12:51 PM 0
Share

If I put:

 Transform.rotation.eulerAngles = new Vector3(xRotationObject.transform.rotation.x, yRotationObject.transform.rotation.y, transform.rotation.z);

I get an error saying that an object reference is required to access non-static member 'UnityEngine.Transform.rotation'; I already put static on the GameObjects but it's still giving me the error.

avatar image KellyThomas · Dec 14, 2013 at 01:03 PM 0
Share
 transform.rotation = Quaternion.Euler(
                        xRotationObject.transform.rotation.eulerAngles.x,
                        yRotationObject.transform.rotation.eulerAngles.y,
                        transform.rotation.eulerAngles.z);
avatar image alemimi · Dec 14, 2013 at 01:13 PM 0
Share

Oh finally, thank you that worked!

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

17 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

Related Questions

Mouse won't lock 1 Answer

Door script not working when door is turned 90 degrees 1 Answer

Scroll Bar 1 Answer

FindGameObjectsWithTag not working 1 Answer

Animation not working 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