【发布时间】:2014-01-20 18:48:08
【问题描述】:
当我想将Camera从原点移动到目标位置时,它看起来很僵硬。那么如果可以根据偏移量设置移动速度,怎么办?
【问题讨论】:
-
你试过什么?如果您不发布任何代码,我们将无法知道您在做什么,也无法提出任何改进建议。
-
请描述一下你的尝试!
标签: unity3d transform gameobject lerp
当我想将Camera从原点移动到目标位置时,它看起来很僵硬。那么如果可以根据偏移量设置移动速度,怎么办?
【问题讨论】:
标签: unity3d transform gameobject lerp
关于这个问题有一个很好的教程,通常在unity网站上以of all tutorials开头描述。在Survival Shooter Tutorial里面有说明如何让摄像机在移动的同时平滑地移动到目标位置。
这是移动相机的代码。创建一个脚本,将其添加到相机,然后将您要移动到的GameObject 添加到脚本占位符中。它将自动保存转换组件,如脚本中的设置。 (在我的例子中,它是生存射击教程的玩家):
public class CameraFollow : MonoBehaviour
{
// The position that that camera will be following.
public Transform target;
// The speed with which the camera will be following.
public float smoothing = 5f;
// The initial offset from the target.
Vector3 offset;
void Start ()
{
// Calculate the initial offset.
offset = transform.position - target.position;
}
void FixedUpdate ()
{
// Create a postion the camera is aiming for based on
// the offset from the target.
Vector3 targetCamPos = target.position + offset;
// Smoothly interpolate between the camera's current
// position and it's target position.
transform.position = Vector3.Lerp (transform.position,
targetCamPos,
smoothing * Time.deltaTime);
}
}
【讨论】:
您可以使用iTween 插件。如果您希望您的相机与您的对象一起移动,即顺利地跟随它。您可以使用smoothFollow 脚本。
【讨论】:
您是否尝试过使用 Vector3.MoveTowards?您可以指定要使用的步长,应该足够平滑。
http://docs.unity3d.com/Documentation/ScriptReference/Vector3.MoveTowards.html
【讨论】:
public Vector3 target; //The player
public float smoothTime= 0.3f; //Smooth Time
private Vector2 velocity; //Velocity
Vector3 firstPosition;
Vector3 secondPosition;
Vector3 delta;
Vector3 ca;
tk2dCamera UICa;
bool click=false;
void Start(){
ca=transform.position;
UICa=GameObject.FindGameObjectWithTag("UI").GetComponent<tk2dCamera>();
}
void FixedUpdate ()
{
if(Input.GetMouseButtonDown(0)){
firstPosition=UICa.camera.ScreenToWorldPoint(Input.mousePosition);
ca=transform.position;
}
if(Input.GetMouseButton(0)){
secondPosition=UICa.camera.ScreenToWorldPoint(Input.mousePosition);
delta=secondPosition-firstPosition;
target=ca+delta;
}
//Set the position
if(delta!=Vector3.zero){
transform.position = new Vector3(Mathf.SmoothDamp(transform.position.x, target.x, ref velocity.x, smoothTime),Mathf.SmoothDamp( transform.position.y, target.y, ref velocity.y, smoothTime),transform.position.z);
}
}
【讨论】: