简体   繁体   English

Unity3d中的C#罗盘旋转脚本

[英]C# compass rotation script in Unity3d

I have a compass object which rotates and moves along with the camera. 我有一个指南针对象,它随相机旋转和移动。 Thus the rotation and position of the compass object is adjusted along with the player-controlled camera. 因此,指南针对象的旋转和位置与玩家控制的摄像机一起被调整。 Now, having attached a needle to my compass - I would like to have the needle point north. 现在,在将指南针固定在指南针上之后,我想将指针指向北方。

That is easily done but having the needle point to a northern object. 这很容易做到,但是让针指向北部的物体。 But the needle now doesn't follow the plate (compass object) it is placed on. 但是,现在针不再跟随其放置的板(罗盘对象)。 I would thus like the needle to point accordingly. 因此,我希望针尖相应地指向。 In other word, rotate singly along the y-axis. 换句话说,沿y轴单独旋转。

I don't need code - I just need a broad idea of how to do that. 我不需要代码-我只需要一个广泛的想法。 I would really appreciate any help :) 我真的很感谢您的帮助:)

I finally figured it out :D It was much easier than expected... Here is the code if anyone should ever need it: 我终于弄清楚了:D这比预期的要容易得多...如果有人需要它,这是代码:

using UnityEngine;
using System.Collections;

public class Compass : MonoBehaviour 
{
    void Update () 
    {
         transform.localRotation = Quaternion.Euler(0, 360-transform.root.rotation.eulerAngles.y, 0);
    }
}

I would try to project the vector pointing from compass location to target onto the plane where the needle should lay on. 我会尝试将从罗盘位置指向目标的向量投影到应该放置针的平面上。 Vector.ProjectOnPlane might be helpful. Vector.ProjectOnPlane可能会有所帮助。 Something like this should work (not yet tested): 这样的事情应该起作用(尚未测试):

Vector3 dir = compassPos - targetPos;
Vector3 needleDir = Vector3.ProjectOnPlane(dir, compassNormal);

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

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