ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-02-04 19:58:28  阅读:182  来源: 互联网

标签:Unity3D void Vector3 transform Update position using 向量 调头


 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 () {
	
	}
}

标签:Unity3D,void,Vector3,transform,Update,position,using,向量,调头
来源: https://blog.csdn.net/rong11417/article/details/122785348

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有