Search Unity

Rotate gameobject to where camera is facing.

Discussion in 'Scripting' started by DarkEcho, Oct 23, 2017.

  1. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    What I want to do, is for the gameObject to snap/face the direction (y axis) the camera is looking when a button is pressed.

    The camera is a child of the gameObject.

    The Camera
    The camera is tilted 15deg on x axis looking at the object. The camera will rotate around the object on the cameras Y axis.

    The Object
    The object will face the cameras facing direction on the Y axis. Example: The camera is currently at 15,90,0. The object, 0,0,0. When a button is pressed in this instance, the objects new current rotation will be 0,90,0.
     
    Last edited: Oct 24, 2017
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Can you describe your goal more clearly? I'm not clear on what you want it to do.
     
  3. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Sorry for my vagueness..
    What I want to do, is for the gameObject to snap/face the direction (y axis) the camera is looking when a button is pressed.

    The camera is a child of the gameObject.
     
  4. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
  5. MattM_Unity

    MattM_Unity

    Unity Technologies

    Joined:
    Aug 18, 2017
    Posts:
    45
    Hello DarkEcho!

    Try something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RotateToCamera : MonoBehaviour
    6. {
    7.     private GameObject MainCamera;
    8.     private float Speed = 10.0f;
    9.     private bool isRotating = false;
    10.  
    11.     void Awake ()
    12.     {
    13.         MainCamera = GameObject.FindGameObjectWithTag("MainCamera");
    14.     }
    15.  
    16.     void Update ()
    17.     {
    18.         //Set your input right here to start the rotation
    19.         if (Input.GetKeyDown(KeyCode.Space))
    20.             isRotating = !isRotating; //Starts the rotation
    21.  
    22.         if (isRotating) //Check if your game object is currently rotating
    23.             SetRotate(this.gameObject, MainCamera);
    24.  
    25.         //When your child game object and your camera have the same rotation.y value, it stops the rotation
    26.         if (toRotate.transform.rotation.eulerAngles.y == camera.transform.rotation.eulerAngles.y)
    27.             isRotating = !isRotating;
    28.     }
    29.  
    30.     void SetRotate(GameObject toRotate, GameObject camera)
    31.     {
    32.         //You can call this function for any game object and any camera, just change the parameters when you call this function
    33.         transform.rotation = Quaternion.Lerp(toRotate.transform.rotation, camera.transform.rotation, Speed * Time.deltaTime);
    34.     }
    35. }
    For this to work, make sure your camera is tagged "MainCamera". If you want to assign another tag, simply change the name of the tag in Awake.

    Simply drop this script on your game object, and press Space, your object will rotate to where the camera is facing :) You don't even have to make your game object a child of the camera for this script to work!
     
    PythonPeopleexist likes this.
  6. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    I have tried adapting it to my needs, but no luck. The problem is, it is rotating the object on all axis.

    The Camera
    The camera is tilted 15deg on x axis looking at the object. The camera will rotate around the object on the cameras Y axis.

    The Object
    The object will face the cameras facing direction on the Y axis. Example: The camera is currently at 15,90,0. The object, 0,0,0. When a button is pressed in this instance, the objects new current rotation will be 0,90,0.
     
    Last edited: Oct 24, 2017
  7. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
    Got it!

    Code (CSharp):
    1. transform.rotation = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0);
    Only problem is as the camera is a child of the object that is rotating, the camera will follow. The camera cannot move...
     
    awlok, unity_2YELxeezlMRVBg and ow3n like this.
  8. MattM_Unity

    MattM_Unity

    Unity Technologies

    Joined:
    Aug 18, 2017
    Posts:
    45
    Could you post a few screenshots of your setup in the Scene view? It would allow me to understand your situation a bit better :)
     
  9. MattM_Unity

    MattM_Unity

    Unity Technologies

    Joined:
    Aug 18, 2017
    Posts:
    45
    A possible solution would be to unchild the camera from the game object, do the rotation then make the camera a child of the game object once the rotation is done.
     
  10. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233

    Ok, so I would have to do the above If i want to keep the Camera as a child, otherwise I will need to have the camera as a independent object?

    In which case I will need to script it so the camera follows the gameObject?
     
  11. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    233
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I personally find in nearly every situation that having the camera be separate from the followed object to be easier (to deal with, alter, etc.. ) :)
     
    Joe-Censored likes this.
  13. VRDK

    VRDK

    Joined:
    Jan 9, 2019
    Posts:
    2
    Hey Matthieu ! Ashley from Success Advisor pointed me in this direction, and as the other comments says, this is not for a singular axis, but all the axis in the rotation :)


    As seen on the picture - the camera is centered at 0,0,0 while the spaceship is at the outer rim around 0,-83,0. The idea is that the cameras rotation control the position of the spaceship, meaning the spaceship rides around inside the sphere dodging asteroids and gathering fuel.

    The Problem -

    the spaceship that rides around the inside of the sphere should be able to point in the direction which the camera rotates towards. Which essentially means that the spaceship should only rotate in its Y-axis.

    Example -


    Here is my example to give the bigger picture. The camera rotates (-15, -20, 0) as you can see in the Game View it goes towards the upper left. The spaceship should therefore rotate in its Y-axis only towards that same point/direction.

    :) ive tried for a long time both with a lot of different solutions which mostly did not work. I acknowledge that im not that well versed in rotation math and in that direction - so any help is much welcomed !

    Kind Regards,
    Lars.

    Edit - The forums wont recognize my link to imgur when using the "Insert Image" nor can i direct a link to the imgur site.
     
  14. MattM_Unity

    MattM_Unity

    Unity Technologies

    Joined:
    Aug 18, 2017
    Posts:
    45
    Hello VRDK!

    Not going to lie, It's hard to visualize the problem without screenshots (considering you wrote your explanations with your screenshots in mind) and it doesn't help that this forum post is over a year old ;)

    You could send me your screenshots directly in my inbox (or find a way to post them on the forums) and I'll try to give it a look as soon as I can.
     
  15. VRDK

    VRDK

    Joined:
    Jan 9, 2019
    Posts:
    2