- Home /
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();
}
}
}
Your answer
Follow this Question
Related Questions
How can I make stop and object from rotating? 0 Answers
How to move the parent object to a child of one of its children? 2 Answers
check if children are visible, if not destroy parent? 1 Answer
How to find an object in children 1 Answer
Parent GameObject dosen't move anymore if the children have a rigidbody 2 Answers