Search Unity

How to find 3d Coordinates on a Sphere and set it perpendicular to the center

Discussion in 'Scripting' started by laurelhach, Aug 25, 2015.

  1. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Hi Guys,

    I've attached some pictures to illustrate the problem.
    Basically what I am trying to achieve is:
    1. I have a camera in the world that can rotate (1st person camera) with limited direction in all axes. The camera doesn't translate, only rotate.
    2. I would like the plane to always be facing the camera (Both Vector3.Forward are aligned) but at a certain distance (radius = 0.7f in my example).
    3. The global idea is to create an effect on top of the camera (like a vignette).
    There are some links I found:
    But I can't make them work in my situation (I never have all the axes correctly positioned).

    This part of my code creates two issues (In update):
    The cube is not parented to the camera in that example.
    • Horizontally, the cube moves in the opposite directions (the camera looks right, cube goes left). But stays on the sphere and is perpendicular to it
    • Vertically, the cube doesn't move a lot.
    Code (CSharp):
    1.  
    2.             float x = (float)(Camera.main.transform.position.x + 0.7f * System.Math.Cos(Camera.main.transform.rotation.y) * System.Math.Sin(Camera.main.transform.rotation.y));
    3.             float y = (float)(Camera.main.transform.position.y + 0.7f * System.Math.Sin(Camera.main.transform.rotation.x) * System.Math.Sin(Camera.main.transform.rotation.y));
    4.             float z = (float)(Camera.main.transform.position.z + 0.7f * System.Math.Cos(Camera.main.transform.rotation.y));
    5.             transform.position = new Vector3(x,y,z);
    6.             transform.LookAt (Camera.main.transform.position);
    7.  
    If I parent the cube to the camera (with no Update()), the cube behaves correctly on the horizontal axe, but not vertical.
    On the vertical axe, the cube goes too high and too low and does not cover the camera view correctly. Would it be because of the offset between the camera and the cube?

    Thanks for helping!

    upload_2015-8-25_9-32-30.png

    upload_2015-8-25_9-45-53.png
     
  2. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
  3. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Yes but with all axes. Up, down, left and right :)
     
  4. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    I don't understand, what I posted behaves the same no matter how you rotate the camera, it's essentially like with a billboard particle.

    Can you try this and tell me what in situations is it not working correctly?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestScript : MonoBehaviour {
    5.     public float distance = 10;
    6.     public Vector3 rotationOffset; //if needed
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         Transform target = Camera.main.transform;
    16.  
    17.         transform.position = target.position + target.forward * distance;
    18.  
    19.         transform.rotation = Quaternion.Euler(Quaternion.LookRotation(target.position - transform.position).eulerAngles + rotationOffset);
    20.     }
    21. }
    22.  
     
  5. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Hi and thanks,

    The plane follows the camera as wanted.
    But there is a delay in the plane's update rotation.

    I have to leave for a while, will try to resume when I come back.

    Thanks for your support!
     
  6. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Adnan,

    So your script works.
    I have added a script to be able to look around when pressing the right mouse button.
    Everything goes well when I look around but when I release the mouse button, the cube takes 0.5s before moving back to his default position.

    I use lerp to move back the camera to the origin.

    Can you also help on this?

    Thanks!
     
  7. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    Can you make a web build demostration, or record the behaviour?
    The script I gave you has no kind of delays, so im not sure what you mean.
     
  8. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Hi,

    Here is the link to the webplayer version.
    I don't know if you can actually play this version, but you can download it.
    https://www.dropbox.com/sh/t1omcb8dtz7rttd/AADK6ole7a8byPenoHffIMo5a?dl=0

    The camera in the webplayer has a wider angle, but the issue remains the same.
    If you move the camera using the right mouse button and release it when you reach the extreme left / up / down / right position, you'll see that the plane won't follow it completely.

    Thanks!
     
  9. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    Is the camera a child of another object by any chance? Can you post the script which returns the camera to the start position?
     
  10. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    In my game, the Camera is a child of another object.
    In the Webplayer the camera is not.

    Here is the code I use.
    Code (CSharp):
    1. private IEnumerator ReplaceCam(){
    2.         bool _isLerping = true;
    3.         float _timeStartedLerping = Time.time;
    4.         float timeTakenDuringLerp = 1; // time to complete the sequence
    5.  
    6.         while (_isLerping ) {
    7.  
    8.             float timeSinceStarted = Time.time - _timeStartedLerping;
    9.             float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
    10.  
    11.             playerCam.transform.rotation = Quaternion.Slerp(playerCam.transform.rotation, camRotation, percentageComplete);
    12.             //Debug.Log(percentageComplete + " " + Quaternion.Angle(playerCam.transform.rotation, camRotation));
    13.  
    14.             float distance = Quaternion.Angle(playerCam.transform.rotation, camRotation);
    15.  
    16.             if( distance <= 0.1f)
    17.                 _isLerping = false;
    18.  
    19.             yield return null ;
    20.         }
    21.  
    22.         // We reset the camera value
    23.         playerCam.transform.rotation = camRotation;
    24.         playerCam.transform.position = camPosition;
    25.         _mouseAbsolute = Vector2.zero;
    26.         isCameraMovingBack = false;
    27.     }
     
  11. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    hmm... lets try a small trick there, just to be 100% sure theyre executing in the right order (if this fixes your issue it will be easier to pin it down)

    in your camera script, add a reference to the script i gave you, and assign the object from the scene.
    Then, move the Update() contents from the script i gave you (the 3 lines) to a public function (e.g. refresh())
    Finally, in Update just call it every frame, and here before yielding null, call it as well.
     
  12. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    First thanks for your help :) I really appreciate it.

    I have updated the webplayer to reflect the changes.
    I also built a PC version (feel free not to use it if you think there could be a virus on the *.exe) in the same folder.

    The "come back" now works fine. The move using the right mouse button show a delay now.

    Does it help ?
     
  13. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    You always have a delay that simply should not be there! Is your project huge? Can you just pack the camera and this move script in a separate project and upload it? It will make troubleshooting a matter of minutes
     
  14. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    It is actually right now.
    I have an empty project with the camera a light and the plane.

    Edit: Project has been updated and zipped!
     
    Last edited: Aug 26, 2015