简体   繁体   English

我需要使用 c# 将一个游戏对象旋转到 Unity 中的另一个游戏对象,但我希望旋转仅在 z

[英]I need to rotate a gameObject towards another gameobject in Unity with c#, but i want the rotation to be only in z

I have done this program but when i move my gameobject(the seccond one) the rotation of the first gameObject in y starts to go randomly from 90 to -90.我已经完成了这个程序,但是当我移动我的游戏对象(第二个)时,y 中第一个游戏对象的旋转开始从 90 到 -90 随机旋转。

public GameObject target; 
public float rotSpeed;  
void Update(){  
Vector3 dir = target.position - transform.position; 
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.Euler(new Vector3(0, 0, angle));
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, rotSpeed * Time.deltaTime);
}

The simplest way for this is not going through Quaternion but Vector3 instead.最简单的方法不是通过Quaternion而是通过Vector3

What many people don't know is: You can not only read but also assign a value to eg transform.right which will adjust the rotation in order to make the transform.right match with the given direction!许多人不知道的是:您不仅可以读取,还可以为例如transform.right赋值,它会调整旋转以使transform.right与给定的方向匹配!

So what you can do is eg所以你可以做的是例如

public Transform target; 
public float rotSpeed;  

void Update()
{  
    // erase any position difference in the Z axis
    // direction will be a flat vector in the global XY plane without depth information
    Vector2 targetDirection = target.position - transform.position;

    // Now use Lerp as you did
    transform.right = Vector3.Lerp(transform.right, targetDirection, rotationSpeed * Time.deltaTime);
}

If you need this in local space since eg your object has a default rotation on Y or X you can adjust this using如果您需要在本地空间中使用它,因为例如您的对象在 Y 或 X 上具有默认旋转,您可以使用

public Transform target; 
public float rotSpeed;  

void Update()
{  
    // Take the local offset
    // erase any position difference in the Z axis of that one
    // direction will be a vector in the LOCAL XY plane
    Vector2 localTargetDirection = transform.InverseTransformDirection(target.position);

    // after erasing the local Z delta convert it back to a global vector
    var targetDirection = transform.TransformDirection(localTargetDirection);

    // Now use Lerp as you did
    transform.right = Vector3.Lerp(transform.right, targetDirection, rotationSpeed * Time.deltaTime);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使 GameObject 指向 Unity 中的另一个 GameObject? - How can I make a GameObject point towards another GameObject in Unity? Unity3d:将一个游戏对象向另一个游戏对象的前向矢量旋转 - Unity3d: Rotate a gameobject towards forward vector of another gameobject Unity C#GameObject与另一个GameObject发生冲突 - Unity C# GameObject collides with another GameObject 如何根据 Unity 中的另一个游戏对象调整旋转? - How can I adjust rotation based on another gameObject in Unity? 如何根据运动方向旋转带有 RigidBody 的游戏对象? 团结 | 3D | C# - How do I rotate a GameObject with a RigidBody based on it's movement direction? Unity | 3D | C# Unity 5.6-C#-使用AddComponent将游戏对象添加到另一个游戏对象<T> - Unity 5.6 - C# - Adding a GameObject to another GameObject with AddComponent<T> 使用统一 c# 我想打开或关闭一个包含所有组件的游戏对象 - Using unity c# i want to toggle on or off a GameObject with all of it's components inside unity/c#:来自另一个游戏对象的数据 - unity/c#: data from another gameobject 如何使游戏对象在跳跃时旋转,然后使其与另一个游戏对象碰撞,然后平稳返回其原始旋转状态? - How can I make a gameObject rotate while jumping, then once it collides with another gameObject, return smoothly to its original rotation? 在Unity中停止GameObject的Z旋转 - Stop Z rotation of gameObject on touch in Unity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM