问题描述
我是Unity的新用户,想在这个网站上建立相机视窗:
有一个Curb Camera Motion的例子。
任何想法,如何实现这一点?
我使用这个代码:
使用UnityEngine;
using System.Collections;
public class CameraController:MonoBehaviour {
public GameObject player;
public Vector3 min;
public Vector3 max;
private Vector3 offset;
void Start()
{
offset = transform.position - player.transform.position;
}
void LateUpdate()
{
Vector3 newPos = player.transform.position + offset;
newPos.x = Mathf.Clamp(x,min.x,max.x);
newPos.y = Mathf.Clamp(x,min.y,max.y);
newPos.z = Mathf.Clamp(x,min.z,max.z);
transform.position = newPos;不幸的是,摄像机移动不正确,所以摄像机不能正常工作。任何想法,如何创建一个摄像头窗口? 解决方案这里的主要问题是您需要在屏幕空间中检查您的目标位置。其中对象在屏幕坐标中。然后如果目标在屏幕坐标中的窗口之外,移动相机。这里使用的主要功能是
Camera.main.WorldToScreenPoint
pre>
以下是一个基本类,应该模仿文章中的效果。这可以改进很多,但应该足以让你开始正确的方向。
使用UnityEngine;
using System.Collections;
public class CurbCam:MonoBehaviour
{
public Transform targetPosition;
public float camWindowDimension;
Vector2 targetScreenPos;
float deltaX
float deltaZ;
//用于初始化
void Start()
{
}
//每帧调用一次更新
void Update()
{
//将目标位置转换为2D
targetScreenPos = Camera.main.WorldToScreenPoint(targetPosition.position);
if(targetScreenPos.x>(Screen.width / 2)+ camWindowDimension)
{
deltaX = targetScreenPos.x - ((Screen.width / 2)+ camWindowDimension );
transform.position = new Vector3(transform.position.x + deltaX,transform.position.y,transform.position.z);
}
if(targetScreenPos.x<(Screen.width / 2) - camWindowDimension)
{
deltaX = targetScreenPos.x - / 2) - camWindowDimension);
transform.position = new Vector3(transform.position.x + deltaX,transform.position.y,transform.position.z);
}
if(targetScreenPos.y>(Screen.height / 2)+ camWindowDimension)
{
deltaZ = targetScreenPos.y - / 2)+ camWindowDimension);
transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z + deltaZ);
}
if(targetScreenPos.y<(Screen.height / 2) - camWindowDimension)
{
deltaZ = targetScreenPos.y - / 2) - camWindowDimension);
transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z + deltaZ);
}
}
}
I'm new in Unity and want to create a camera-window like on this website:http://gamasutra.com/blogs/ItayKeren/20150511/243083/Scroll_Back_The_Theory_and_Practice_of_Cameras_in_SideScrollers.php#h.elfjc4ap4hpe There is a example with Curb Camera Motion. I want to make a camera-window which pushes the camera position as the player hits the window edge.
Any ideas, how to realize this?
I used this code:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
public Vector3 min;
public Vector3 max;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
Vector3 newPos = player.transform.position + offset;
newPos.x = Mathf.Clamp(x, min.x, max.x);
newPos.y = Mathf.Clamp(x, min.y, max.y);
newPos.z = Mathf.Clamp(x, min.z, max.z);
transform.position = newPos;
}
}
Unfortunately, the camera is moving not correct. Any ideas, how to create a camera-window?
解决方案 The main issue here is that you need to check your targets position in screen space. Where the object is in screen coordinates. And then move your camera if the target is out of the window in "screen coordinates". The main function to be used here is
Camera.main.WorldToScreenPoint
Following is a basic class that should mimic the effect in the article. This can be improved alot but should be enough to get you started in the right direction.
using UnityEngine;
using System.Collections;
public class CurbCam : MonoBehaviour
{
public Transform targetPosition;
public float camWindowDimension;
Vector2 targetScreenPos;
float deltaX;
float deltaZ;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
//convert target pos to 2D
targetScreenPos = Camera.main.WorldToScreenPoint (targetPosition.position);
if (targetScreenPos.x > (Screen.width/2) + camWindowDimension)
{
deltaX = targetScreenPos.x - ((Screen.width/2) + camWindowDimension);
transform.position = new Vector3(transform.position.x + deltaX, transform.position.y, transform.position.z);
}
if (targetScreenPos.x < (Screen.width/2) - camWindowDimension)
{
deltaX = targetScreenPos.x - ((Screen.width/2) - camWindowDimension);
transform.position = new Vector3(transform.position.x + deltaX, transform.position.y, transform.position.z);
}
if (targetScreenPos.y > (Screen.height/2) + camWindowDimension)
{
deltaZ = targetScreenPos.y - ((Screen.height/2) + camWindowDimension);
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + deltaZ);
}
if (targetScreenPos.y < (Screen.height/2) - camWindowDimension)
{
deltaZ = targetScreenPos.y - ((Screen.height/2) - camWindowDimension);
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + deltaZ);
}
}
}
这篇关于如何在Unity3D中创建相机窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!