Search Unity

Turning on the spot

Discussion in 'Getting Started' started by Meruvian, Mar 12, 2018.

  1. Meruvian

    Meruvian

    Joined:
    Sep 19, 2015
    Posts:
    8
    So I have an object I call 'FollowCube' and I am trying to make it move around the camera (like a controlled orbit). I.e: when I move the mouse right, the cube will move to the right in orbit of the camera, and the camera (through the use of Quartenion.RotateTowards will slowly try to catch up until it is facing the cube.

    I have been trying to do this for months but have failed, even trying to use mathf.sin to create this affect but it is not working as intended (I will post the code below)


    The Code on the Cube
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CubeFollow : MonoBehaviour {
    6.  
    7.     public float moveSpeed = 40f;
    8.     public Camera MainCamera = null;
    9.  
    10.     float xAim;
    11.     float yAim;
    12.     public float zPos;
    13.  
    14.     float orbitDist = 30f;
    15.  
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         Cursor.visible = false;
    20.    
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.         xAim += Input.GetAxisRaw ("Mouse X") * 1 * Time.deltaTime;
    27.         yAim += Input.GetAxisRaw ("Mouse Y") * moveSpeed * Time.deltaTime;
    28.  
    29.  
    30.         transform.position = MainCamera.transform.position + new Vector3 (Mathf.Sin(xAim) * 10 , yAim ,5);
    31.         //transform.position +=  new Vector3(0,0,Input.GetAxisRaw ("Mouse X") * MainCamera.transform.position.y + -Mathf.Sin(Input.GetAxisRaw ("Mouse X")) * moveSpeed * Time.deltaTime);
    32.  
    33.  
    34.         print (Mathf.Sin (xAim));
    35.  
    36.         //transform.rotate
    37.  
    The Code on the Camera
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LookCursor : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     Vector2 screenMousePos = Vector2.zero;
    9.     //Event thisEvent = Event.current;
    10.     public Camera thisCamera = null;
    11.     GameObject FollowCube = null;
    12.     public Vector3 worldMousePos = Vector3.zero;
    13.     public float turnSpeed = 0.1f;
    14.  
    15.  
    16.  
    17.     void Start () {
    18.  
    19.     FollowCube = GameObject.Find ("FollowCube");
    20.        
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.         print (worldMousePos);
    27.         Quaternion DestRot = Quaternion.LookRotation (FollowCube.transform.position - transform.position);
    28.         transform.rotation = Quaternion.RotateTowards (transform.rotation, DestRot, turnSpeed * Time.deltaTime);
    29.                        
    30.     }
    31.  
    32.  
    33.  
    34. }
    if this is not the right way to go about it then suggestions would be a great help! I have only used this method as a result of reading and research and trying to teach myself things I can't readily find on tutorials.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    The code looks about right to me. How exactly is this not behaving as you desire?
     
  3. Meruvian

    Meruvian

    Joined:
    Sep 19, 2015
    Posts:
    8
    Well it's only moving left and right at the moment, (along the X-axis of the cube). which initially is fine, the porblem occurs when you move the mouse too far left, and the sine wave switches to -1 when you move the mouse left, the cube goes in the opposite direction? it's hard to explain but I can make a video to show if that's easier?

    also, the cube isn't orbiting the camera, when i place the mathf.sin(xAim) in the z axis of:

    Code (CSharp):
    1. transform.position = MainCamera.transform.position + new Vector3 (Mathf.Sin(xAim) * 10 , yAim ,5);
    it starts acting REALLY weird and moves all about the place!

    In my understanding, placing this sine calculation, using the xAim to increment both the X and Z positions of the cube should have causes it to move in a circle around the camera.

    it did not.

    the code I have attached above is the version i reverted back to when the Z increment code I tried turned out not to work
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, so I was focusing on the camera look-at code. Always best to separate your problems... get the camera to follow an object you move around manually; and as a separate issue, get the cube to do its mouse-orbit thing without worrying about whether the camera follows it. When both parts work correctly, they'll work together.

    Unfortunately, I now realize I have no idea what you're actually trying to do with the mouse-orbit thing. :) In general, to make one object orbit another, you don't add stuff to its position... you instead keep track of the radius and angle, and then on each frame simply calculate the position with a bit of trig:

    Code (csharp):
    1.  transform.position = new Vector3(r * Mathf.Cos(angle), 0, r. * Mathf.Sin(angle));
    Your mouse code should probably just add to the angle (and r, the radius, you maybe never change at all).
     
  5. Meruvian

    Meruvian

    Joined:
    Sep 19, 2015
    Posts:
    8
    First of all, thank you so much for your response and I'm sorry mine was SOO long, I've been asleep and then went to work!

    I think you're completely right about using the radius, I've never thought of that, what I was trying to do was use the orbit concept in this video here:



    I'll still have to do some research on how, maybe brush up on my trig, but this has pointed me in the right direction, thank you!
     
    JoeStrout likes this.