x


Rotation script ineffective on objects with children

I have been working on a script that will allow the player to click on objects in a scene to get a closer look at the actual object. When this script is attached to an object, that object is then selectable.

Once a selectable object is clicked, the object is temporarily moved to a different position (the EmptyPosition game object), along with the main camera (and the "player" is turned off), where the player can rotate the object to get a better view. They can then click "Return" on the GUI and it will take the player back to the actual location in the scene where the character is.

Testing this script, it works fine when attaching it to a single cube. The problem arises when I attach it to an object that has children beneath it. Nothing happens. Is it possible to make this script treat that parent object and its children all as one object?

using UnityEngine;
using System.Collections;

public class MoveObjectToRotate : MonoBehaviour {


 public GameObject EmptyPosition;
 public float speed = 3f;
 public float lerpSpeed = 3f;
 public float minDistance = 3f;
 public float maxDistance = 10f;

 Color currentColor;
 Color hoverColor = Color.green;

 bool FocusObject = false;

 Vector3 InitialPosition;
 Vector3 InitialRotation;
 Vector3 MoveTo;
 GameObject player;

 private float xDeg;
 private float yDeg;
 private float zDeg = 0f;
 private float cameraZoom;
 private Quaternion fromRotation;
 private Quaternion toRotation;


 void Start () 
 {
 InitialPosition = transform.position;
 InitialRotation = transform.eulerAngles;

 MoveTo = EmptyPosition.transform.position;

 player = GameObject.FindWithTag("Player");

 currentColor = renderer.material.color;

 }

 void Update () 
 {
 if (FocusObject)
 { 
 HandleInputs();
 CalculateObjectRotation(); 
 }

 }

 void HandleInputs()
 {
 if(Input.GetMouseButton(1)) 
     {
         xDeg -= Input.GetAxis("Mouse X") * speed;
         zDeg -= Input.GetAxis("Mouse Y") * speed;
     }

 if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0)
     {
 xDeg -= Input.GetAxis("Horizontal") * speed;
 zDeg -= Input.GetAxis("Vertical") * speed;
  }
 }

 void CalculateObjectRotation()
 {
 fromRotation = transform.rotation;
        toRotation = Quaternion.Euler(yDeg,xDeg,zDeg);
        transform.rotation = Quaternion.Lerp(fromRotation, toRotation, Time.deltaTime  * lerpSpeed);
 }

 void MoveToRotateField()
 {
 player.active = false;
 transform.position = MoveTo;
 Camera.main.orthographic = true;
 Camera.main.orthographicSize = 2.25f;
 Camera.main.transform.position = new Vector3(MoveTo.x - 5, MoveTo.y, MoveTo.z);
 Camera.main.transform.eulerAngles = new Vector3(0, 90, 0);
 }

 void ReturnToPlayer()
 {
 transform.position = InitialPosition;
 transform.eulerAngles = InitialRotation;
 FocusObject = false;
 Camera.main.orthographic = false;
 player.active = true;
 }

 void OnGUI()
 {
 if(FocusObject)
 {
 if(GUI.Button(new Rect(120, 20, 100, 25), "Return"))
 ReturnToPlayer();
 }
 }

 void OnMouseOver()
 {
 if(!FocusObject)
 renderer.material.color = hoverColor;
 }

 void OnMouseExit()
 {
 if(!FocusObject)
 renderer.material.color = currentColor;
 }

 void OnMouseDown()
 {
 if(!FocusObject)
 {
 FocusObject = true; 
 renderer.material.color = currentColor;
 MoveToRotateField();
 }
 }
}
more ▼

asked Jul 13 '12 at 09:46 PM

Stridulent gravatar image

Stridulent
4 4 5

(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x723
x419
x184

asked: Jul 13 '12 at 09:46 PM

Seen: 243 times

Last Updated: Jul 13 '12 at 09:46 PM