仅将相机移动到某个位置

时间:2017-01-02 02:53:43

标签: c# unity3d camera

https://scontent.fmgf1-2.fna.fbcdn.net/v/t34.0-12/15870843_1543174992374352_1359602831_n.gif?oh=dc048c9e04617007c5e82379fd5a9c1a&oe=586CC623

你能看到这个gif中的蓝色区域吗?当玩家向左走多时,我想阻挡相机,看不到蓝色区域。这是我用来移动相机的代码:

public class configuracoesDaCamera : MonoBehaviour {

Vector3 hit_position = Vector3.zero;
Vector3 current_position = Vector3.zero;
Vector3 camera_position = Vector3.zero;
float z = 0.0f;
public static bool bDedoNoHud;

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        hit_position = Input.mousePosition;
        camera_position = transform.position;

    }
    if (Input.GetMouseButton(0))
    {
        current_position = Input.mousePosition;
        LeftMouseDrag();
    }
    Debug.Log("bDedoNoHud" + bDedoNoHud);
}

void LeftMouseDrag()
{
    if (!bDedoNoHud)
    {
        // From the Unity3D docs: "The z position is in world units from the camera."  In my case I'm using the y-axis as height
        // with my camera facing back down the y-axis.  You can ignore this when the camera is orthograhic.
        current_position.z = hit_position.z = camera_position.y;

        // Get direction of movement.  (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
        // anyways.  
        Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);

        // Invert direction to that terrain appears to move with the mouse.
        direction = direction * -1;

        Vector3 position = camera_position + direction;

        transform.position = position;
    }           
}

}

1 个答案:

答案 0 :(得分:2)

你的意思是设定边界?

This should help you

将它放在update()