动态绳子生成

Rob.cs

using UnityEngine;
using System.Collections;

public class Rob : MonoBehaviour {
    
    public Transform sPt;
    public Transform ePt;
    public float thic = 0.1f;
    public Material robMat;
    enum DisState{X ,Y, Z, XY, YZ, XZ, XYZ};
    // Use this for initialization
    void Start () 
    {
        
    }
    
    // Update is called once per frame
    void LateUpdate () 
    {
        if (sPt == null || ePt == null)
        {
            return;
        }
        float len = getDistant(sPt.position, ePt.position, DisState.XYZ);

        transform.position = sPt.position;
        transform.localScale = new Vector3(thic, thic, len);
        transform.LookAt(ePt);
//      MeshRenderer mr = transform.GetComponent<MeshRenderer>();
//      if (robMat != null)
//      {
//          mr.material = robMat;
//      }
//      mr.material.mainTextureScale = new Vector2(1.0f, len * 4.0f);
    }
    
    float getDistant(Vector3 pt1, Vector3 pt2, DisState dir)
    {
        if(dir == DisState.XYZ)
        {
            return Mathf.Sqrt(Mathf.Pow(pt1.x - pt2.x,2)
                               + Mathf.Pow(pt1.y - pt2.y,2)
                               + Mathf.Pow(pt1.z - pt2.z,2));
        }
        else if(dir == DisState.X)
        {
            return Mathf.Abs(pt1.x - pt2.x);
        }
        else if(dir == DisState.Y)
        {
            return Mathf.Abs(pt1.y - pt2.y);
        }
        else if(dir == DisState.Z)
        {
            return Mathf.Abs(pt1.z - pt2.z);
        }
        else if(dir == DisState.XY)
        {
            return Mathf.Sqrt(Mathf.Pow(pt1.x - pt2.x,2)
                               + Mathf.Pow(pt1.y - pt2.y,2));
        }
        else if(dir == DisState.YZ)
        {
            return Mathf.Sqrt(Mathf.Pow(pt1.z - pt2.z,2)
                               + Mathf.Pow(pt1.y - pt2.y,2));
        }
        else if(dir == DisState.XZ)
        {
            return Mathf.Sqrt(Mathf.Pow(pt1.x - pt2.x,2)
                               + Mathf.Pow(pt1.z - pt2.z,2));
        }
        else 
        {
            return -1.0f;
        }
    }
}

推荐阅读更多精彩内容