【问题标题】:Camera collision in unity统一的相机碰撞
【发布时间】: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,我也尝试过使用刚体。我还是一个初级程序员,只是在业余时间写代码,来解释我缺乏知识。

【问题讨论】:

标签: c# unity3d collision-detection


【解决方案1】:

OnCollisionEnter 有点棘手,我相信当它与动态刚体(即非运动学)交互时,你会得到最好的结果。如果您希望它检查与墙壁的碰撞,在这种情况下,相机和墙壁都没有动态刚体,那么只需使用 OnTriggerEnter。

如果您正在尝试制作一个 RPG 风格的角色控制器,并且相机碰撞代码是为了帮助防止相机穿过墙壁,那么我相信您可以使用 raycast 完成这项工作(通过从相机向玩家拍摄 raycast ) 而不是使用 OnTrigger。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多