【发布时间】:2017-01-19 12:56:51
【问题描述】:
我正在为一个学校项目创建一个第三人称游戏,但我遇到了一些碰撞问题:我有一个带相机的玩家小时候有一个球体碰撞器。当相机与房屋等任何风景物体发生碰撞时,它应该缩小。一旦它离开碰撞情况,它应该回到原来的位置(它的局部 y 应该是 4.5)。现在我在静止不动时遇到以下问题:相机不断离开并进入对象的对撞机,导致它不断放大和缩小。这会导致看起来非常有问题的相机移动。有什么办法可以解决这个问题吗?
我使用了以下代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamMovement : MonoBehaviour
{
public GameObject Parent;
//Checks if the camera collides with something
void OnTriggerStay(Collider other)
{
//When colliding, the camera moves up and back from the player object
transform.position += new Vector3(0, 0.2f, -0.2f);
}
void Update()
{
//makes sure the camera always looks at the player object
transform.LookAt(Parent.transform);
//Moves the camera back to the normal (local) position
if (transform.localPosition.y > 4.5f)
{
transform.position += new Vector3(0, Time.deltaTime * -4f, Time.deltaTime * 4f);
}
}
}
相机与某物碰撞时的样子:http://imgur.com/a/7ot9R
【问题讨论】:
-
您可以在播放器对象上放置一个标签,然后获取它的相机对撞机并执行
if(!other == playerCollider){zoomOut();}如果这解决了您的问题,请告诉我,我会写下它作为正确的答案。 -
如果任何答案解决了您的问题,请考虑接受它作为正确的答案。 (接受答案有助于未来访问此页面的访问者)
标签: c# unity3d camera collision