自动校准模型中心点

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

public class ceshi2 : MonoBehaviour
{

    void Start()
    {
        obj = GameObject.Find("Cube").transform;

        foreach (Transform tr in transform)
        {
            tr.gameObject.AddComponent<MeshCollider>();
        }

     
    }
  Transform obj;
    void Update()
    {
        TarRaycast();

        if (V.Count==30)
        {
            Vector3 center=Vector3.zero;
            foreach (Vector3 v in V)
            {
                center += v;
            }
            center = center / 30;
            obj.position = center;

            float f = transform.localScale.x;

            transform.parent = obj;
            obj.position = Vector3.zero;
            obj.localScale = Vector3.one / f;
        }





    }
    Vector3 V1, V2;

    Vector3 第一次碰撞点;
    bool 是第一次碰撞;

    List<Vector3> V = new List<Vector3>();


    Vector3 mp;//鼠标位置
    Transform targetTransform;//点选的物体


    bool TarRaycast()
    {
        mp = Input.mousePosition;
        targetTransform = null;

        RaycastHit hitInfo;

        V1 = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100));
        if (!是第一次碰撞)
            V2 = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100));
        else
        {
            V2 = 第一次碰撞点 + new Vector3(Random.Range(-30, 30), Random.Range(-30, 30), Random.Range(-30, 30));
        }
        bool grounded = Physics.Linecast(V1,V2,out hitInfo);

        if (grounded)
        {
            错误射线 = -10000;
            if (是第一次碰撞 == false)
            {
                第一次碰撞点 = hitInfo.point;
                是第一次碰撞 = true;
            }
            Debug.LogError("碰到了:" + hitInfo.point);
            V.Add(hitInfo.point);
            Debug.DrawLine(V1, V2, Color.red, 100f);

        }
        else
        {
            错误射线++;
            Debug.LogError("没碰到");
            if (错误射线 > 500)
            {
                transform.localScale += new Vector3(0.05f,0.05f,0.05f);
            }
        }
       
        return false;

    }
    int 错误射线;
}

 

posted @ 2018-08-06 16:28  哎呦不能错喔  阅读(297)  评论(0编辑  收藏  举报