Hi,
I want use a tranform.lookat function for look an object but with a different z rotation?
Plz Thank you Sry for my bad english
Hi,
I want use a tranform.lookat function for look an object but with a different z rotation?
Plz Thank you Sry for my bad english
There's no version of LookAt which operates on a different axis.
To get around this, you can either:
a) rotate the object after performing the LookAt, like so:
transform.LookAt( target );
transform.Rotate( -90, 0, 0 );
or,
b) make your visible object the child of a "dummy" empty game object, and rotate the child object by 90 degrees in the desired direction. Then perform the LookAt function on the empty parent game object.
hope this helps!
Z-Rotation, globally, determines the "up" direction (twisting around the line-of-sight axis). So, easy, just pass in the current up vector as the optional second argument:
transform.LookAt(target, transform.up);
I assume you're making 2d game?
You can use something like that
var targetPoint: GameObject // gameobject to look to
function Update()
{
transform.LookAt(targetPoint);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0); // lock x and z axis to zero
}
Try using transform.eulerAngles . With that you can also lock your Z axis to other different degree. For example :
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 75);
If you mean you want the LookAt feature but leave z fixed say z = fixedZ, I was able to do this with
transform.LookAt(Vector3(target.position.x , target.position.y , fixedZ));
or if you want the z to stay level with the transform
transform.LookAt(Vector3(target.position.x , target.position.y , transform.position.z));
Hi
I’ve been having a similar problems that I’m pulling my hair out with.
I have an object, say A, placed at the center which is intended to always point towards another orbiting object, say B. A needs to rotate along its Z-axis, with the X and Y axes being zero, furthermore, it is B’s X and Y changing coordinates that are used to compute A’s direction. I have used the following script:
Vector3 targetPosition = new Vector3(target.transform.position.x, target.transform.position.y, this.transform.position.z);
transform.LookAt(targetPosition);
transform.Rotate(0, 90, 0);
Now this does the job, however the problem I have is that while A is rotating along its Z-axis, Y rotation flips from 0 to 180 (or sometimes -180) causing A to flip upside down. This happens as soon as the X coordinate of B changes from being positive to negative and vice versa. I’ve tried isolating the z rotation through another related object using A’s localEulerAngles but the flip in the Y axis distorts the z rotation in one instance going from -90 straight to 90, skipping 180 degrees.
If anyone has any suggestions I’d be much obliged.
Thank you