Unity球面星球行走 向量投影 物体在面内按轴锁定LookAt旋转


		//球体
		GameObject ball;
		//用到的投影方法
        public static Vector3 cast(Vector3 vector, Vector3 PlaneNorm)// vector要投影的向量,投影到以PlaneNorm为法线的面
        {
            return vector + PlaneNorm * (Vector3.Dot(vector, PlaneNorm) * -1);
        }
        void LookAt(GameObject target)
        {
	        transform.rotation = Quaternion.LookRotation(cast((target.transform.position - transform.position).normalized, -Physics.gravity), -Physics.gravity);
        }
        public void Rotate(float x)
        {
            transform.rotation.SetLookRotation(Quaternion.AngleAxis(x, -Physics.gravity) * transform.forward, -Physics.gravity);
        }
		void Rotate()
		{
			Physics.gravity = (ball.transform.position - transform.position).normalized;
			transform.rotation = Quaternion.FromToRotation(transform.up, -Physics.gravity) * transform.rotation;
		}

你可能感兴趣的:(unity,游戏引擎)