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 AironeneroTechnologies · Nov 23, 2014 at 09:19 AM · c#rotationrotate object

Rotation around an axis, how unity handle this?

Im trying to rotate a transform on his axis via C#. But when executed this really not move... Can someone explain me why? How quaternions works? Im using this code (later ill add a non frame rate dependent mode.)

 public GameObject SunComponent;
 
 void Start()
     {
         LightComponenet = SunComponent.GetComponent<Light>();
     }
 
 void Update()
     {
         SunRotation = SunComponent.transform.rotation.x;
         SunRotation = SunRotation++;
     }

I was expecting the objec rotating really fast, instead it simply not moves... Why?

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

3 Replies

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

Answer by AironeneroTechnologies · Nov 23, 2014 at 06:17 PM

Solved this problem by myself (after two hours of study about quaternoins and euler angles.)

The solution is really simple: converting quaternions to vector3 angles (eulers) and applying the result. In code is really simple:

 public class SunTime : MonoBehaviour {
     public Vector3 SunRotation = new Vector3 (1, 0, 0);
     public GameObject SunComponent;

     //Each update i add 1 euler rotation on the X axis.
     void Update()
     {
         SunComponent.transform.Rotate(SunRotation);
     }
 }
 
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 FairGamesProductions · Nov 23, 2014 at 10:44 AM

Because "SunRotation" is NOT the object's transform. After you increase SunRotation, you need to apply it to the gameObject:

  SunComponent.transform.rotation.x = SunRotation;
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 AironeneroTechnologies · Nov 23, 2014 at 05:22 PM 0
Share

Visual studio says: impossible to modify a value that doesnt is a variable.

(and i already know that.)

I think i should use quaternions (as writed in the question).

avatar image
0

Answer by Kannonfodder · Nov 23, 2014 at 12:14 PM

I'm assuming SunRotation is defined as an int somewhere.

 SunRotation = SunComponent.trasnform.rotation.x 

The above assigns the value of x to SunRotation as it is not a reference, but a value type. Any changes to SunRotation won't be applied back to the value in SunComponent as it has copied the value to your new variable SunRotation.

To re-apply the value you need to manually set the value in SunComponent:

 SunComponent.transform.rotation.x = SunRotation;

or even faster just :

 SunComponent.transform.rotation.x++;

As a quick note on formatting, variables should be camelCase and methods/classes should be CamelCase

This Code will just make the sun rotate about itself however. If you're trying to get it to rotate about a point its slightly more complicated. To get the sun to move in an arc over the centre of the terrain i implemented the following and attached the script to my sun object: public class SunMove : MonoBehaviour {

     GameObject terrain;
     public float speed;
     float terrainLength;
     float terrainWidth;
     float angle;
     float radius;
     float cos;
     float sin;
     Vector3 centre;
     float sinRad;
     float cosRad;
     // Use this for initialization
     void Start()
     {
         terrain = GameObject.FindGameObjectWithTag("Terrain");
         if (speed == null)
         {
             speed = 0.01f;
         }
         terrainLength = terrain.GetComponent<Terrain>().terrainData.size.z;
         terrainWidth = terrain.GetComponent<Terrain>().terrainData.size.x;
         transform.position = new Vector3(terrainWidth * 0.5f, 0, 0);
         angle = 0f;
         centre = new Vector3(terrainWidth * 0.5f, 0, terrainLength * 0.5f);
         radius = Vector3.Distance(centre, transform.position);
     }
 
     // Update is called once per frame
     void Update()
     {
         if (speed != 0)
         {
             angle += speed;
             cos = Mathf.Cos(angle);
             sin = Mathf.Sin(angle);
             sinRad = sin * radius;
             cosRad = cos * radius;
             gameObject.transform.position = new Vector3(centre.x, sinRad + centre.y, cosRad + centre.z);
             
         }
         else
         {
             transform.position = new Vector3(centre.x, 125, -125);
         }
         gameObject.transform.LookAt(centre);
     }
 }
Comment
Add comment · Show 4 · 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 CHPedersen · Nov 23, 2014 at 12:19 PM 0
Share

Technically correct, but please note two things:

1: The OP uses C#. In C#, you cannot directly assign a value to transform.rotation.x, because rotation is not a public variable, but a property. You'll get the CS1612 compiler error stating "Cannot modify return value of... etc.".

2: Rotations in Unity are represented as quaternions, which are complex numbered matrices that can uniquely represent all rotations without gimble lock, but which do not make intuitive sense. Accessing and working with its individual values as though they were Euler angles does not make sense.

avatar image Kannonfodder · Nov 23, 2014 at 12:22 PM 0
Share

I should have actually looked at the variable (property) in question before answering, didn't realise it was accessor only...oops

avatar image FairGamesProductions · Nov 23, 2014 at 02:45 PM 0
Share

This is why I hate C#. It's unnecessarily complicated... JS is so much simpler.

avatar image AironeneroTechnologies · Nov 23, 2014 at 05:24 PM 0
Share

In UnityScript (and not JS) you not have interfaces and other advanced thing that are really useful when making advanced relations with different classes.

Anyway the code not works, im pretty sure i need to use quaternion, but how?

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

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

Related Questions

Flip over an object (smooth transition) 3 Answers

transform.Rotate will not work 1 Answer

Question on Forcing a Specific Rotation 1 Answer

How to rotate a GameObject around another object according to the mouse cursor position in C# 0 Answers

How do I rotate an object to the cursor?Where is the mistake in the code? 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