- Home /
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
Three Game Objects are needed for it.
Game$$anonymous$$anager ( any GameObject).
Rotating Object 1 ( any mesh object )
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;
}
}
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.
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);
}
}
one more thing,
add this script to camera
objects should have collider
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
![](https://koobas.hobune.stream/wayback/20220613120615im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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