【问题标题】:Raycast jittering when used to place object in world用于在世界中放置对象时的光线投射抖动
【发布时间】:2021-03-17 15:31:21
【问题描述】:

我有一个脚本,可以将光线从鼠标投射到地面层,以显示将要放置对象的位置。它是一个城市建设者,应该在放置之前显示房屋的轮廓。轮廓只是一个带有透明材料的预制件。

当预制件随着鼠标移动(即将放置)时,它会晃动,我不知道为什么。有谁知道如何解决这个问题?

第 8 层也是地面。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BlueprintScript : MonoBehaviour
{   
    RaycastHit hit;
    Vector3 movePoint;
    public GameObject prefab;
 
    void Start()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray, out hit, 50000.0f, (1 << 8)))
        {
             transform.position = hit.point;
        }
    }

    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
        if (Physics.Raycast(ray, out hit, 50000.0f, (1 << 8)))
        {
              transform.position = hit.point;
        }

        if (Input.GetMouseButton(0))
        {
              Instantiate(prefab, transform.position, transform.rotation);
              Destroy(gameObject);
        }
    }
}

【问题讨论】:

  • 对我来说看起来不错,也许你的预制件中有一些地面层游戏对象

标签: c# unity3d raycasting


【解决方案1】:

尝试以下操作:

  • 禁用概述的房屋碰撞器(如果有)。 (如果它现在运行良好,那么您的光线可能正在击中轮廓的房子。)

  • 打印hit.point,看看它是否真的有抖动。

  • 如果有抖动,请确保您的相机没有移动。

  • Update 替换为 FixedUpdate。您最好在 FixedUpdate 方法中进行物理处理。 (并分别在 Update 方法中检查您的输入事件。)

  • 如果它仍然抖动,请检查您的物理设置,最后为您的 transform.position 分配添加一个 Vector3.Lerp

    transform.position = Vector3.Lerp(transform.position, hit.point, 0.07F);

我认为第四个是你的答案,祝你好运。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 2014-11-04
    相关资源
    最近更新 更多