Search Unity

3D Rotating R.T.S Camera

Discussion in 'Scripting' started by Vampyr_Engel, May 27, 2018.

  1. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Hi I was wondering how you can use say the arrow keys to rotate an object and how you could use the mouse to move the camera to pan East and West and North and South and use the wasd keys to do the same but only if you click on an sprite. I wish to do this around a spherical planetary map be nice to see some example code please?
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RPGMoveCamera : MonoBehaviour {
    6.  
    7.     // Update is called once per frame
    8.     void Update () {
    9.  
    10.         if ( Input.GetKey ( KeyCode.W ) )
    11.  
    12.         {
    13.             gameObject.transform.Translate ( -2, 0 , -2);
    14.         }
    15.  
    16.         if ( Input.GetKey ( KeyCode.S ) )
    17.  
    18.         {
    19.             gameObject.transform.Translate ( 2, 0 , 2);
    20.         }
    21.  
    22.         if ( Input.GetKey ( KeyCode.A ) )
    23.  
    24.         {
    25.             gameObject.transform.Translate ( 2, 0 , -2);
    26.         }
    27.  
    28.         if ( Input.GetKey ( KeyCode.D ) )
    29.  
    30.         {
    31.             gameObject.transform.Translate ( -2, 0 , 2);
    32.         }
    33.  
    34.    
    35.     }
    36. }
    37.  
    This is code I have used for an isometric camera but the principle is the same for any kind of third person camera, create your main camera, create and empty gameobject, then make the camera a child of the empty gameobject and use either translate or rotate to move the empty gameobject rather than the camera itself.

    The camera will then act like it's a proper third person camera, if you wanted to camera orbit an object like a car for example you would just write some rotation code and attach that code to the empty, the camera should then rotate around the empty gameboject as the game object rotates.

    Hope that makes sense, just to let you know you'll need to adjust this code, I've used cartesian co-ordinates in this, just a matter of changing the co-ordinates to fit your project. This will work if you want to pan the camera with WASD, the exact same principle though applies for rotation.
     
    Last edited: May 27, 2018
  3. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449

    Oh OK I guess you can modify this so that the mouse can do the same actions as well as the WASD keys? And thanks though if you could so how me how to do the rotation code I would be most grateful if not OK thank you very much anyway
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    It shouldn't be too difficult to look up the mouserotation code, all you need to do is attach the script to the empty gameobject after.
     
  5. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449
    Oh OK thanks
     
  6. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    The easiest solution would be a double pivot system.

    So have an empty gameobject at the planets core. This will be rotated to move the camera around the world.

    Make another empty gameobject, make this a child of the one at the worlds core, this is what your camera will look at. Place this on the planets surface directly above its parent.

    Now put your camera as a child of the look at point, place it at the default height you want & make sure its looking at its parent point.

    Now write a script that takes 2 gameobjects, one being the point at the worlds centre the other being the point on the surface.

    To move the camera around rotate the point at the centre. The Transform component has an easy to use rotate function. To move forward or backwards rotate around the right axis, to move left or right use the forward axis. To rotate the camera rotate around the up axis.

    For moving the mouse near the edge you can use the Screen class to get the width & height of the screen & use Input.mouseposition to get the mouses screen position, check which edge it is near to move that way.

    If your world isnt a flat sphere you will want to do a raycast against the terrain to move the look at point up & down to avoid the camera clipping.

    If you need any clarification or more help just ask :)
     
  7. Vampyr_Engel

    Vampyr_Engel

    Joined:
    Dec 17, 2014
    Posts:
    449

    Thank you I may need to have help especially to code it