【发布时间】:2020-09-03 10:57:18
【问题描述】:
我不知道如何检查碰撞,这是我的相机移动脚本:
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),但似乎没有用,我做错了什么吗?所有对象都有colliders,我也尝试过使用刚体。我还是一个初级程序员,只是在业余时间写代码,来解释我缺乏知识。
【问题讨论】:
-
这能回答你的问题吗? Collision detection not working unity
标签: c# unity3d collision-detection