unity获得运动物体前后时刻的位置坐标pos值

比如获得一个小球运动中前后的坐标

分别用RunStart, RunNext.

1,在Start()里面给RunStart获得初值。

2. Update() 里面RunNext在需要记录的时刻取值。

----------------------------------

3。 在RunStart = RunNext.

 

 

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

public class LineMark : MonoBehaviour {



	private GameObject clone;  
	private LineRenderer line;  
	private int i;  
	public GameObject obs;  
	public GameObject run;  
	Vector3 RunStart;
	Vector3 RunNext;

	// Use this for initialization
	void Start () {
		RunStart = run.transform.position;
		clone = (GameObject)Instantiate(obs, run.transform.position, run.transform.rotation);//克隆一个带有LineRender的物体   
		line = clone.GetComponent<LineRenderer>();//获得该物体上的LineRender组件  
//		//line.SetColors(Color.blue, Color.red);//设置颜色  
//		//line.SetWidth(0.2f, 0.1f);//设置宽度  
		i = 0;
	}

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

		RunNext = run.transform.position;

		if (RunStart != RunNext) {
			i++;
			line.SetVertexCount(i);//设置顶点数 
			line.SetPosition(i-1, run.transform.position);
			
		}

		RunStart = RunNext;



		if (Input.GetMouseButtonDown(0))  
		{  
			clone = (GameObject)Instantiate(obs, obs.transform.position, transform.rotation);//克隆一个带有LineRender的物体   
			line = clone.GetComponent<LineRenderer>();//获得该物体上的LineRender组件  
			line.SetColors(Color.blue, Color.red);//设置颜色  
			line.SetWidth(0.2f, 0.1f);//设置宽度  
			i = 0;  
		}  
		if (Input.GetMouseButton(0))  
		{  
			i++;  
			line.SetVertexCount(i);//设置顶点数  
			line.SetPosition(i - 1, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 15)));//设置顶点位置   
		}  




	}  
}

 

 

 

 

Unity中,如果你想要获取两个物体相交区域的位置坐标,你需要根据物体的形状和相交的类型来进行计算。通常,这种计算比较复杂,因为需要考虑物体的碰撞体(Collider)类型和可能的变换(Transform)。以下是获取两个物体相交区域坐标的一种基本思路: 1. 确定相交区域:首先需要检测两个物体是否相交,可以使用Unity的Physics类中的方法,如`Physics.OverlapBox`、`Physics.OverlapSphere`、`Physics.OverlapCapsule`等来检测简单的几何形状物体的相交;或者使用`Physics.Linecast`、`Physics.Raycast`等射线检测方法来检测射线与物体的相交。 2. 获取相交点:如果两个物体相交,通常会得到一个包含交点信息的数组或者直接是交点的坐标。例如,`OverlapBox`会返回所有相交点的数组。 3. 计算相交区域的平均位置:如果有多个交点,可以通过计算所有交点的平均位置来得到相交区域的一个粗略估计位置。可以通过将所有交点的坐标相加后,再除以交点数量来获取。 以下是一个简化的代码示例,假设两个物体都是简单的BoxCollider形状,并且相交: ```csharp using UnityEngine; public class IntersectionDetector : MonoBehaviour { private void DetectIntersection(GameObject object1, GameObject object2) { BoxCollider boxCollider1 = object1.GetComponent<BoxCollider>(); BoxCollider boxCollider2 = object2.GetComponent<BoxCollider>(); if (boxCollider1 != null && boxCollider2 != null) { Collider[] hits = Physics.OverlapBox(boxCollider1.bounds.center, boxCollider1.size / 2, Quaternion.identity, 1 << object2.layer); if (hits.Length > 0) { Vector3 intersectionCenter = Vector3.zero; foreach (Collider hit in hits) { if (hit.gameObject == object2) { // 假设物体相交点只在盒体的中心,这里简单地取中心点作为示例 intersectionCenter += boxCollider2.bounds.center; } } intersectionCenter /= hits.Length; Debug.Log("相交区域的估计位置为:" + intersectionCenter); } } } } ``` 请注意,这个示例非常简化,实际情况可能更为复杂,尤其是当物体的形状更为复杂或相交区域并不是简单的几何形状时。在复杂的场景中,可能需要使用更高级的数学计算和物理模拟来获得精确的相交区域位置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值