Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to rotate camera around children objects

Discussion in 'Scripting' started by Goldensnitch, Nov 21, 2016.

  1. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    I have been working anatomy type application. But i face this problem doesn't know how to solve this issue. Whenever i try to rotate my camera around children object my script doesn't work. Can someone help me with this. Thank you. Please view video below.




    Code (CSharp):
    1. //this is the camera rotate script
    2. using System;
    3. using UnityEngine;
    4. public class mongol : MonoBehaviour
    5. {
    6.     public GameObject Obj = null;
    7.     public float minX = -360.0f;
    8.     public float maxX = 360.0f;
    9.     public float minY = -45.0f;
    10.     public float maxY = 45.0f;
    11.     public float sensX = 100.0f;
    12.     public float sensY = 100.0f;
    13.     float rotationY = 0.0f;
    14.     float rotationX = 0.0f;
    15.     float MouseX;
    16.     float MouseY;
    17.     void Update()
    18.     {
    19.         var x = Input.GetAxis("Mouse X");
    20.         var y = Input.GetAxis("Mouse Y");
    21.         if (x != MouseX || y != MouseY)
    22.         {
    23.             rotationX += x * sensX * Time.deltaTime;
    24.             rotationY += y * sensY * Time.deltaTime;
    25.             rotationY = Mathf.Clamp(rotationY, minY, maxY);
    26.             MouseX = x;
    27.             MouseY = y;
    28.             Obj.transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    29.         }
    30.     }
    31. }
     
  2. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    How is Obj being set? When you select the skull, does the Obj reference change to that Gameobject?
     
  3. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    nope i dont get what you saying sorry
     
  4. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    You're rotating the Obj gameobject in the script. How is the Obj gameobject set? There must be another script setting variables, otherwise it will always be null.
     
  5. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    oh ok It got 304 children and 1 parent object. I have attached this rotation script to camera and i attached parent object as its target. But whenever i select children object its doesn't zoom into center of the screen and can't rotate selected object in the center of the screen. I share you video please help me out.