Linecast在Vector3之前停止

时间:2015-05-06 03:42:02

标签: c# unity3d

我正在尝试做一个Linecast,但它太准确了,所以它不按我想要的方式工作。

我似乎无法弄清楚如何从点A进行投射,并使0.001f远离点B(点A意味着{ {1}}和点camera.transform.position表示代码段中的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); } } 在点之前停止?

修改

前视图

在前视图中并非所有Handles都显示:

Front

后视图

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

Back

完整代码

CreatureCreatorEditor.cs

Linecast

Creator.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(){

    }
}

2 个答案:

答案 0 :(得分:0)

您需要一个偏移量。你应该从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);
        }
    }

答案 1 :(得分:0)

我喜欢它是如何工作的,我只需要让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);
            }
        }
    }
}