Reputation: 1632
I have an object, which has the following code:
[SerializeField]
Transform target;
[SerializeField]
Vector3 defaultDistance = new Vector3(0f, 2f, -10f);
[SerializeField]
float distanceDamp = 2f;
[SerializeField]
Vector3 defaultPos;
Transform myT;
// Use this for initialization
void Awake () {
myT = transform;
defaultPos = this.transform.position;
}
// Update is called once per frame
void LateUpdate () {
Vector3 toPos = target.position + (target.rotation * defaultDistance);
toPos = new Vector3(toPos.x, 1f , toPos.z);
Vector3 curPos = Vector3.Lerp(myT.position, toPos, distanceDamp * Time.deltaTime);
myT.position = curPos;
}
What I'm trying to achieve:
I want the object(cube in this example, but then GUI in VR) to follow Main Camera's rotation, meaning I want the object to Lerp into the center of the camera's view when it rotates. When removing the line of code toPos = new Vector3(toPos.x, 1f , toPos.z);
and adding the LookAt
function, If I use a GUI/Cube/etc, it works perfectly, the object is always moving towards the center of the camera. BUT - I don't want the object to move towards the center when I rotate my camera Up/Down on the X-Axis. Thus, I wrote that line toPos = new Vector3(toPos.x, 1f , toPos.z);
and now I have a bug, when I rotate the camera on the X-Axis (Meaning, up/down), after some degrees of X-axis rotation, the object starts moving on Z-Axis towards the camera's position, Instead I don't want it to move at all when I rotate upwards/downwards.
I have worked it around with eulerAngles, but It's not the solution I'm looking for because I need to define a range of degrees which still makes the object lift a bit up/down. I want it to move only on circular way around the camera on any rotation. (I've the circular movement code, but I wasn't able to implement it to my needs above)
Here's the eulerAngles code:
if (target.rotation.eulerAngles.x >= 350 || target.rotation.eulerAngles.x <= 10)
{
Move the object towards the camera's view
Vector3 toPos = target.position + (target.rotation * defaultDistance);
Vector3 curPos = Vector3.Lerp(myT.position, toPos, distanceDamp * Time.deltaTime);
myT.position = curPos;
}
else //Return the object to it's position and Y=1 (goes down/up accordingly to camera)
{
Vector3 curPos = Vector3.Lerp(myT.position, new Vector3(myT.position.x, 1, myT.position.z), distanceDamp * Time.deltaTime);
myT.position = curPos;
}
EDIT: This is what I've achieved so far, The cube doesn't center on the middle yet, but it circly rotates around the camera's rotation.
Vector3 toPos = target.position + target.rotation * new Vector3(defaultDistance.x, 0.0f, defaultDistance.z);
float multiplier = Mathf.Sqrt(defaultDistance.x * defaultDistance.x + (defaultDistance.z) * (defaultDistance.z));
//multiplier /= Mathf.Sqrt(toPos.x * toPos.x + toPos.z * toPos.z);
toPos.x *= -multiplier;
toPos.y = target.position.y + defaultDistance.y;
toPos.z *= -multiplier ;
toPos.z -= 70;
Vector3 curPos = Vector3.Lerp(myT.position, toPos, distanceDamp * Time.deltaTime);
myT.position = curPos;
EDIT 2:
Almost perfect now with toPos.z -= 60;
Upvotes: 0
Views: 1572
Reputation: 585
Would this do the trick?
Vector3 targetPos = target.position;
targetPos.y = transform.position.y;
Quaternion newRotation = Quaternion.LookRotation(targetPos - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, 0.5f);
Upvotes: 1