我试着做一个直线,但它太精确了,所以它不是我想要的工作方式。
我似乎不知道如何执行从point A转换而来的行,并阻止0.001f远离点B (Point A意为camera.transform.position,以及point B意为代码片段中的handle )。
foreach(Vector3 handle in handles){
if(!Physics.Linecast(camera.transform.position, handle)){
Handles.FreeMoveHandle(handle, Quaternion.identity, 0.001f, Vector3.zero, Handles.DotCap);
}
}那么,从相机到重点,我如何才能让Linecast停下来呢?
编辑
前视镜
在前面的视图中,并不是所有的句柄都显示:

后视镜
在后面的视图中,几乎所有的句柄都在那里:

全码
CreatureCreatorEditor.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using Creature;
[CustomEditor(typeof(Creator))]
public class CreatureCreatorEditor : Editor {
HashSet<Vector3> handles = new HashSet<Vector3>();
void OnEnable(){
Creator t = (Creator)target;
Mesh mesh = t.GetComponent<MeshFilter>().sharedMesh;
if (mesh != null) {
Vector3[] vertices = mesh.vertices;
Vector3 lp = t.transform.position;
foreach (Vector3 v in vertices) {
Vector3 p = (lp - v);
handles.Add(new Vector3 (p.x, -p.z, p.y));
}
}
}
public void OnSceneGUI(){
Handles.color = Color.red;
Camera camera = Camera.current;
foreach(Vector3 handle in handles){
Vector3 point = camera.WorldToViewportPoint(handle);
if(point.x > 0 && point.x < 1 && point.y > 0 && point.y < 1){
// float dist = Vector3.Distance(camera.transform.position, handle);
// Vector3 fwd = camera.transform.TransformDirection(handle);
Vector3 newBPos = new Vector3(handle.x - 0.001f, handle.y - 0.001f, handle.z - 0.001f);
if(!Physics.Linecast(camera.transform.position, newBPos)){
Handles.FreeMoveHandle(handle, Quaternion.identity, 0.001f, Vector3.zero, Handles.DotCap);
}
}
}
}
public override void OnInspectorGUI(){
}
}Creator.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
namespace Creature{
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshCollider))]
public class Creator : MonoBehaviour {
}
}发布于 2015-05-06 17:53:37
我喜欢它的工作原理,我只需要让Y轴正常工作。
public void OnSceneGUI(){
Camera camera = Camera.current;
Handles.color = Color.red;
Vector3 mp = Event.current.mousePosition;
Ray ray = camera.ScreenPointToRay(mp);
RaycastHit hit;
if(Physics.Raycast(ray, out hit)){
Vector3 pos = hit.point;
foreach(Vector3 handle in handles){
if(Vector3.Distance(handle, pos) <= 0.05f){
Handles.FreeMoveHandle(handle, Quaternion.identity, 0.001f, Vector3.zero, Handles.DotCap);
}
}
}
}发布于 2015-05-06 04:42:05
你需要一个补偿。你应该从B位置减去0.001f。
这将对x,y,z位置起作用。
const float newPosOffset = 0.001f;
foreach(Vector3 handle in handles){
Vector3 newBPos = new Vector3(handle.x-newPosOffset ,handle.y-newPosOffset ,handle.z-newPosOffset );
if(!Physics.Linecast(camera.transform.position, newBPos)){
Handles.FreeMoveHandle(handle, Quaternion.identity, 0.001f, Vector3.zero, Handles.DotCap);
}
}https://stackoverflow.com/questions/30067078
复制相似问题