2015-02-07 116 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