2015-02-07 112 views
0

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

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

,如果你想要做的是围绕一个对象

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); 
    } 
+0

是旋转的摄像头,但它需要看到鼠标之间的夹角位置和目标。所以如果我点击对象的右侧,它会向右旋转,或者如果我点击对象的左侧,它将向左旋转。这段代码不正确吗? – 2015-02-07 17:43:59

+0

O wait我可以替换鼠标位置的rotateAround中的vector3吗? – 2015-02-07 17:45:39

+0

我更新了上面的代码,但它仍然无法工作 – 2015-02-07 17:53:15