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

Hi Guys,

I’ve attached some pictures to illustrate the problem.
Basically what I am trying to achieve is:

  • 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.
  • 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).
  • 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.
            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));
            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));
            float z = (float)(Camera.main.transform.position.z + 0.7f * System.Math.Cos(Camera.main.transform.rotation.y));
            transform.position = new Vector3(x,y,z);
            transform.LookAt (Camera.main.transform.position);

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!

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

2266057--151721--upload_2015-8-25_9-45-53.png

If I got you right, you want to achieve this?
http://gfycat.com/LittleFrenchGraysquirrel

Yes but with all axes. Up, down, left and right :slight_smile:

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?

using UnityEngine;
using System.Collections;

public class TestScript : MonoBehaviour {
    public float distance = 10;
    public Vector3 rotationOffset; //if needed

    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {
        Transform target = Camera.main.transform;

        transform.position = target.position + target.forward * distance;

        transform.rotation = Quaternion.Euler(Quaternion.LookRotation(target.position - transform.position).eulerAngles + rotationOffset);
    }
}

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!

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!

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.

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!

Is the camera a child of another object by any chance? Can you post the script which returns the camera to the start position?

In my game, the Camera is a child of another object.
In the Webplayer the camera is not.

Here is the code I use.

private IEnumerator ReplaceCam(){
        bool _isLerping = true;
        float _timeStartedLerping = Time.time;
        float timeTakenDuringLerp = 1; // time to complete the sequence

        while (_isLerping ) {

            float timeSinceStarted = Time.time - _timeStartedLerping;
            float percentageComplete = timeSinceStarted / timeTakenDuringLerp;

            playerCam.transform.rotation = Quaternion.Slerp(playerCam.transform.rotation, camRotation, percentageComplete);
            //Debug.Log(percentageComplete + " " + Quaternion.Angle(playerCam.transform.rotation, camRotation));

            float distance = Quaternion.Angle(playerCam.transform.rotation, camRotation);

            if( distance <= 0.1f)
                _isLerping = false;

            yield return null ;
        }

        // We reset the camera value
        playerCam.transform.rotation = camRotation;
        playerCam.transform.position = camPosition;
        _mouseAbsolute = Vector2.zero;
        isCameraMovingBack = false;
    }

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.

First thanks for your help :slight_smile: 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 ?

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

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!