【发布时间】: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