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 mike66 · Oct 25, 2013 at 08:43 AM · rotationobject

Rotating a selected object

Hi

I have two objects on screen. In play mode I want to be able to select an object and rotate using "r". This rotates the selected object ok, however after selecting 2nd object it now rotates both as opposed to the one I selected.

Any help or code segements appreciated.

Thanx

Comment
Add comment · Show 1
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 LijuDeveloper · Oct 25, 2013 at 10:01 AM 0
Share

Three Game Objects are needed for it.

  1. Game$$anonymous$$anager ( any GameObject).

  2. Rotating Object 1 ( any mesh object )

  3. Rotating Object 1 ( any mesh object )

Game$$anonymous$$anager Script Attach to Game$$anonymous$$anager Object

         using UnityEngine;
         using System.Collections;

         public class Game$$anonymous$$anagerScript : $$anonymous$$onoBehaviour 
         {
             public  GameObject goFan1; // Drag First Rotating  object to this variable
             public  GameObject goFan2;// Drag Secondone Rotating object to this variable
 
             public float speed = 100f;
 
 
             private RotationScript rotateClockWiseScript;
             private RotationScript rotateAntiClockWiseScript;
 
 
 
             void Start () 
             {
                 rotateClockWiseScript = goFan1.GetComponent<RotationScript>();
     
                 rotateAntiClockWiseScript = goFan2.GetComponent<RotationScript>();
     
             }
 
 
             void Update () 
             {
     
                 if( rotateClockWiseScript.boolRotation == true )
                 {
                     goFan1.transform.Rotate(Vector3.back * Time.deltaTime * speed);
                 }
                 if(rotateAntiClockWiseScript.boolRotation == true)
                 {
                     goFan2.transform.Rotate(Vector3.forward * Time.deltaTime * speed);
                 }
     
     
     
 
             }
 
         }


Rotation Script Attach to both rotating object

         using UnityEngine;
         using System.Collections;

         public class RotationScript : $$anonymous$$onoBehaviour 
         {
             public bool boolRotation;
 
             void On$$anonymous$$ouseDown()
             {
                 boolRotation = true;
             }
         }

2 Replies

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

Answer by save · Oct 25, 2013 at 08:52 AM

The reason is that you need a static variable of which object is selected or just one script which takes the clicked object's Transform as a variable. You can compare components, for instance objects Transform or use the instance ID to compare integers.

Having a rotation script on each object could be solved with this addition:

 static var selectedTransform : Transform;
 
 if (selectedTransform==transform)
     // Rotation code here

Having one rotation script anywhere in the scene can be solved like this:

 var selectedTransform : Transform;
 
 if (selectedTransform!=null)
     // Rotation code here, make sure to address selectedTransform in the rotation


How you assign selectedTransform is up to you, it can either be from an OnMouseDown() or a RaycastHit, I prefer raycasting to stay nondependent to platform.

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 yogee · Oct 25, 2013 at 10:14 AM

 Try this,
  
   function Update(){
     var selectObj:Transform;
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;

      if (Physics.Raycast (ray, hit, 100)) {
     selectObt=hit.collider.transform;
             }
     if(Input.GetKey(KeyCode.R)){
     selectObj.Rotate(Vector3(0, Time.deltaTime,0);
     }
 }
Comment
Add comment · Show 2 · 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 yogee · Oct 25, 2013 at 10:20 AM 0
Share
 one more thing,
  add this script to camera
  objects should have collider
avatar image J-R-Wood · Jan 26, 2014 at 11:05 PM 0
Share

Hi Yogee I changed your script to so that it says (Vector3(90, Time.deltaTime,90)); it goes a little crazy but it does flip the object 90deg, what I'm wondering is if we could make it so that it only rotates an object I selected with my mouse? or rather how I would?

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

18 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

Related Questions

How to rotate object slowly on only Z axis 2 Answers

rotation x changes all angles? 3 Answers

object rotation to another vector not changing 0 Answers

problem with object rotation 1 Answer

Return object to position 3 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