简体   繁体   中英

Unity point game object to another only on the Local Y Axis

I would like to have a Gameobject point to another only on the Local Y-axis.

void FixedUpdate()
{
    if(started){
        Quaternion lookRot = Quaternion.LookRotation(target.position - transform.position);
        transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.Euler(transform.eulerAngles.x, lookRot.eulerAngles.y, transform.eulerAngles.z),1);
    }
    
}

If I understand you correctly what you want to achieve is make this object "point at" the target but only allow it to rotate around its local Y axis.

What I would do for this is map the targets actual position onto a mathematical Plane that goes throw this object's position and uses the local Y axis as normal.

Then make your object face towards this mapped position additionally also passing in the local Y axis as the target UP vector which means it rotates only around its local Y axis.

something like eg

void FixedUpdate()
{
    if(started)
    {
        var targetPosition = target.position;

        var plane = new Plane(transform.up, transform.position);
        var mappedTargetPosition = plane.ClosestPointOnPlane(targetPosition);

        Quaternion lookRot = Quaternion.LookRotation(mappedTargetPosition - transform.position, transform.up);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM