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 Stridulent · Jul 13, 2012 at 09:46 PM · rotateparentchildren

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();
  }
  }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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