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?
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);
}
}
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.
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.
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 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.
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