我不知道如何检查冲突,这是我的相机移动脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameracontroller : MonoBehaviour
{
public float movementSpeed;
public float movementTime;
public Vector3 newPosition;
// Start is called before the first frame update
void Start()
{
newPosition = transform.position;
}
// Update is called once per frame
void Update()
{
HandleMovementInput();
}
void HandleMovementInput()
{
if(Input.GetKey(KeyCode.W))
{
newPosition += (transform.forward * movementSpeed);
}
if(Input.GetKey(KeyCode.S))
{
newPosition += (transform.forward * -movementSpeed);
}
if(Input.GetKey(KeyCode.D))
{
newPosition += (transform.right * movementSpeed);
}
if(Input.GetKey(KeyCode.A))
{
newPosition += (transform.right * -movementSpeed);
}
transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
}
}我试过使用void OnCollisionEnter(Collision collision),但似乎不起作用,是不是做错了什么?所有对象都有对撞机,和我也尝试使用刚体。我仍然是一个初学者程序员,只有在我的业余时间的代码,以解释我的知识不足。
发布于 2020-09-03 02:57:18
OnCollisionEnter有点棘手,我相信当它与动态刚体(即非运动学)交互时,会得到最好的效果。如果您想让它检查是否与墙壁碰撞,在这种情况下,相机和墙壁都没有动态刚体,那么只需使用OnTriggerEnter。
如果您正在尝试制作一个RPG风格的字符控制器,而相机碰撞代码是为了防止摄像机从墙上剪裁,那么我相信您可以通过光线投射(通过从摄像机到播放器的光线投射)而不是使用OnTrigger来完成任务。
https://stackoverflow.com/questions/63712627
复制相似问题