- Home /
The vision of a camera (displayed as a 3D Cone) needs to rotate towards the player
The script needs to rotate the gameObject when the Player enters its collision, so that the player is always in the center of the x and z. The camera doesn't move, it can only rotate. When the player leaves the cone, it returns to its original position. The origin of the camera is the top of the cone. Here's my code so far:
private void OnTriggerStay(Collider other)
{
if (other.gameObject.tag == "Ghost")
{
float x = other.gameObject.transform.position.x - transform.position.x;
float z = other.gameObject.transform.position.z - transform.position.z;
// Set x and z to positives if they aren't already
if (x < 0)
{
x *= -1;
}
if (z < 0)
{
z *= -1;
}
transform.parent.gameObject.transform.Rotate(0, CalculateRotation(x, z), 0);
// transform.parent.gameObject.transform.rotation = new Quaternion(0, 0, 0, 0);
}
}
private float CalculateRotation(float x, float z)
{
if (z <= 0)
{
return 0f;
}
float degrees = 0f;
float total = x + z;
float part = z / total;
degrees = part * -90f;
print("X: " + x);
print("Z: " + z);
print("Total: " + total);
print("Degrees: " + degrees * -1);
return degrees;
}
The problem here is that transform.Rotate() keeps triggering, always rotating the camera too far. transform.rotation = new Quaternion() rotates wrongly however, and I can't figure out the pattern of numbers (new Quaternion(90, 0, 90, 0) results in a rotation of (180, 90, 0) for the object, for example).
Does someone have experience / a script regarding the topic? I've already searched online but I'm not sure how to describe the issue, and as such have a hard time finding solutions.
Answer by xxmariofer · Oct 31, 2019 at 12:38 PM
i have some questions, why are you trying to reimplementate the https://docs.unity3d.com/ScriptReference/Transform.LookAt.html yourself? any special needs? and if its because you need to rotate it step by step, cant you use https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html
for example
void Update()
{
Vector3 target = other.transform.position - transform.position;
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
transform.rotation = Quaternion.LookRotation(newDir);
}
Wow, I hadn't come across the LookAt function. Thanks! However, I couldn't figure out how to use RotateTowards, since the rotating object is the parent of the object with the script. Do you know how to fix it?
you need to get the direction from the child, and rotate the parent? if yes, just add a .parent to the last line so you rotate the parent not the child
transform.parent.rotation = Quaternion.LookRotation(newDir);
if you want to rotate the parent, taking into account the parent rotation not the child you would need this
Transform parent = transform.parent;
Vector3 target = other.transform.position - parent.position;
float step = speed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(parent.forward, targetDir, step, 0.0f);
parent.rotation = Quaternion.LookRotation(newDir);