Unity移动物体时,当接近目的地时自动吸附

移动的物体是LuoGan04

目标位置有local position 和旋转确定。Rotation就四元素,其可以把面板调为debug时,再查看。

但这样写会比较麻烦,一个改进的方案是把目的地的位置调好后,将其做成预制体,这时可以面板上把这个零件删除。然后在脚本中同样申请一个public变量,把目的地的预制体赋给它,在通过transform获取其position和rotation这样会比上面第一种方案可能好一点。

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

public class AutoA : MonoBehaviour {


	GameObject LuoGan04;
	Vector3 LuoGan04Aim = new Vector3(37.71f, 4.74f, -22.0f);
	Quaternion LuoGan04Qua = new Quaternion(0.0f, 0.0f, 0.0f, 0.0f);


	// Use this for initialization
	void Start () {
		LuoGan04 = GameObject.Find ("04_LuoGan");
		print (LuoGan04.name );

	}
	
	// Update is called once per frame
	void Update () {

		print ("000000000p" + LuoGan04.transform.localPosition);
		print ("1111111111111p" +LuoGan04Aim);

		print ("2222222222222p" + (LuoGan04.transform.localPosition - LuoGan04Aim).magnitude );
		if (Mathf.Sqrt((LuoGan04.transform.localPosition - LuoGan04Aim).magnitude) < 1.5) {
			LuoGan04.transform.localPosition = LuoGan04Aim;
			LuoGan04.transform.localRotation = LuoGan04Qua;

		}



		
	}
}

Unity移动物体时,当接近目的地时自动吸附_第1张图片

Unity移动物体时,当接近目的地时自动吸附_第2张图片

 

你可能感兴趣的:(unity/blender,VR/AR/MR/XR)