Unity 模拟两个小球位置变化,长度也动态变化

1,新建工程做如下设置。

Unity 模拟两个小球位置变化,长度也动态变化_第1张图片

2. Chang.cs

 

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

public class Change : MonoBehaviour {

	public GameObject sup;
	public GameObject sdown;
	public GameObject cy;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

		Vector3 from = new Vector3(0.0f, 1.0f, 0.0f);//开始轴线的方向
		Vector3 to = (sup.transform.position - sdown.transform.position);//读取轴线的方向
		Vector3 zhou = Vector3.Cross(from, to);//两个向量的叉积
		float angle = Vector3.Angle(from, to);//默认夹角为0-180
		// b 到 a 的夹角
		float sign = Mathf.Sign(Vector3.Dot(zhou.normalized, Vector3.Cross(to.normalized, from.normalized)));//判断夹角的正负
		float signed_angle = angle * sign;
		cy.transform.rotation = Quaternion.AngleAxis(angle, zhou);


		double distance = (Vector3.Distance(sup.transform.position, sdown.transform.position)) / 2.0;
		float Dis = (float)distance;
		// print(distance);
		cy.transform.localScale = new Vector3(cy.transform.localScale.x, Dis, cy.transform.localScale.z);
		cy.transform.position = (sup.transform.position + sdown.transform.position) / 2;

	}
}

 

 

你可能感兴趣的:(unity)