Unity3D 通过向量飞机移动 调头到对象 两物体间距离

 Unity3D 通过向量飞机移动 调头到对象 两物体间距离

Unity3D 通过向量飞机移动 调头到对象 两物体间距离

 

移动

using UnityEngine;
using System.Collections;

public class move : MonoBehaviour {
	public bool upward = true;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (upward && transform.position.y > 5) {
			upward = false;
			transform.localEulerAngles = new Vector3(0, 0 , 180);
		}
		if (!upward && transform.position.y < -5) {
			upward = true;
			transform.localEulerAngles = new Vector3(0, 0 , 0);
		}
		float step = 1.6f * Time.deltaTime; //每帧移动的距离
		transform.Translate(0, step, 0, Space.Self);
	}
}

 调头到目标对象移动

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

public class turn : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
		Debug.Log("x轴向量:" + transform.right.ToString("F3"));
		Debug.Log("y轴向量:" + transform.up.ToString("F3"));
		Debug.Log("z轴向量:" + transform.forward.ToString("F3"));
		Vector3 face = this.transform.up;
		GameObject target = GameObject.Find("peiqi3");
		Vector3 direction = target.transform.position - this.transform.position;
		float angle = Vector3.SignedAngle(face, direction, Vector3.forward);
		this.transform.Rotate(0, 0, angle);
        
    }

    // Update is called once per frame
    void Update()
    {
        float step = 1.6f * Time.deltaTime; //每帧移动的距离
		transform.Translate(0, step, 0, Space.Self);
    }
}

距离

using UnityEngine;
using System.Collections;

public class distance : MonoBehaviour {

	// Use this for initialization
	void Start () {
		GameObject target = GameObject.Find("peiqi3");
		Vector3 p2 = target.transform.position;
		Vector3 p1 = this.transform.position;
		Vector3 distance = p2 - p1;
		Debug.Log("两物体间的距离为:" + distance.magnitude);
		
		Vector3 a = new Vector3(2, 2, 0);
		Vector3 b = new Vector3(-1, 3, 0);
		float angle = Vector3.SignedAngle(a, b, Vector3.forward);
		Debug.Log("两物体间的角度为:" + angle);
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

上一篇:「笔记」一、游戏引擎基础


下一篇:解决创建MVN过慢问题及一些快捷键