围绕中心对象旋转对象

时间:2015-02-07 17:29:46

标签: unity3d

嗨,大家好我写了这个代码来围绕一个中心对象旋转一个对象但我做错了可能有人解释我是什么:)?

public void RotateCamera(GameObject _center)
{
    Vector3 mousePos = camera.WorldToScreenPoint(Input.mousePosition);
    Vector3 centerPos = camera.WorldToScreenPoint(_center.transform.position);
    float angle = Vector3.Angle(centerPos,mousePos);
    Camera.mainCamera.transform.Rotate(centerPos,angle);
}

更新后的代码仍无效:

void Update()
{
RotateCamera(_player);
}

public void RotateCamera(GameObject _center)
{
    float speedMod = 10.0f;
    Vector3 mousePos = camera.WorldToScreenPoint(Input.mousePosition);
    Vector3 centerPos = _center.transform.position;
    Camera.mainCamera.transform.LookAt(centerPos);
    Camera.mainCamera.transform.RotateAround (centerPos,mousePos,20 * Time.deltaTime * speedMod);
}

1 个答案:

答案 0 :(得分:1)

如果您想要做的是围绕对象旋转相机

 public TargetClass target;//the target object
    private float speedMod = 10.0f;//a speed modifier
    private Vector3 point;//the coord to the point where the camera looks at

    void Start () {//Set up things on the start method
        point = target.transform.position;//get target's coords
        transform.LookAt(point);//makes the camera look to it
    }

    void Update () {//makes the camera rotate around "point" coords, rotating around its Y axis, 20 degrees per second times the speed modifier
        transform.RotateAround (point,new Vector3(0.0f,1.0f,0.0f),20 * Time.deltaTime * speedMod);
    }