Unity3d:将一个游戏对象向另一个游戏对象的前向矢量旋转

时间:2019-07-09 19:49:32

标签: c# unity3d vector rotation quaternions

Stream<Object>

因此,我希望我的JavaRDD parsedData = data.map( o -> o.toString() ) .map( s -> { String[] sarray = s.split( " " ); double[] values = new double [sarray.length]; for( int i = 0; i < sarray.length; i++ ) { values[i] = Double.parseDouble( sarray[i] ); } return Vectors.dense( values ); }); parsedData.cache(); BackCube.position = cameraEye.position - cameraEye.forward * 2; float back = cameraEye.position.z - 2f; BackCube.position = new Vector3(BackCube.position.x, BackCube.position.y, back); var lookPosBack = cameraEye.position - BackCube.position; lookPosBack.y = 0; var rotationBack = Quaternion.LookRotation(lookPosBack); BackCube.rotation = Quaternion.Slerp(BackCube.rotation, rotationBack, 1); 的前向矢量旋转。上面的代码着眼于BackCube,而不着眼于cameraEye的前向向量。我希望彼此指向的正向向量彼此相距2个单位。我只能控制cameraEye

1 个答案:

答案 0 :(得分:0)

这有效!

Vector3 direction = Vector3.ProjectOnPlane(cameraEye.forward, Vector3.up);


BackCube.transform.position = cameraEye.position - direction.normalized * 2;


var lookPosBack = cameraEye.position - BackCube.position;
lookPosBack.y = 0;
var rotationBack = Quaternion.LookRotation(lookPosBack);
BackCube.rotation = Quaternion.Slerp(BackCube.rotation, rotationBack, 1);