-
-
Sseakoms
2019-04-15
物体的旋转
-
截图
0赞 · 0采集
-
-
慕粉4076385
2016-09-28
- 重点看看rigidbody
-
0赞 · 0采集
-
-
慕粉1471053985
2016-09-03
- r
-
0赞 · 0采集
-
-
广藿香Patchouli
2016-01-19
- 如果main camera作为player的子节点的话,它会跟着人物旋转,适合第一人称视角,不适合第三人称视角。
-
0赞 · 0采集
-
-
寒杨
2015-08-19
- public float speed;//移动速度
public Rigidbody rb;
public Camera camera;
public float turningSmoothing = 0.3f;
private Vector3 facingDirection ;
private Plane plan;
void Awake ()
{ //垂直与物体y轴的平面
plan = new Plane (transform.up, new Vector3 (2, transform.position.y, 4));
}
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
-
1赞 · 0采集
-
-
寒杨
2015-08-19
- //(系统函数)没daltTime调用一次(通常处理物理运算)
void FixedUpdate ()
{
serFaceingDirection ();
//使物体朝着鼠标旋转
UpdateRotation ();
}
//设置旋转的方向(及鼠标在物体运动平面的坐标(x,0,z))
public void serFaceingDirection ()
{
Vector3 mosePosition = transform.TransformPoint (Input.mousePosition);
facingDirection = ScreenPointToWorldPointOnPlane (Input.mousePosition, plan, camera);
facingDirection.y = transform.position.y;
Debug.DrawLine(transform.position,facingDirection);
}
-
1赞 · 0采集
-
-
寒杨
2015-08-19
- //放回射线与平面相交的点
public Vector3 ScreenPointToWorldPointOnPlane (Vector3 screenPoint, Plane plane, Camera camera)
{
// Set up a ray corresponding to the screen position
Ray ray = camera.ScreenPointToRay (screenPoint);
// Find out where the ray intersects with the plane
float dist=0f;
plane.Raycast(ray ,out dist );//射线与平面相交是放回true,并设置dist的值(及焦点与射线源点的距离)
Vector3 vect=ray.GetPoint(dist);//放回沿射线距离为dist的点
return vect;
}
-
1赞 · 0采集
-
-
寒杨
2015-08-19
- //更新物体的旋转角度(及刚体的角速度)
void UpdateRotation ()
{
// Setup player to face facingDirection, or if that is zero, then the movementDirection
Vector3 faceDir = facingDirection;
// Make the character rotate towards the target rotation
if (faceDir == Vector3.zero) {
GetComponent<Rigidbody> ().angularVelocity = Vector3.zero;
} else {
float rotationAngle = AngleAroundAxis (transform.forward, faceDir, Vector3.up);
//设置刚体的角速度
GetComponent<Rigidbody> ().angularVelocity = (Vector3.up * rotationAngle * turningSmoothing);
}
}
-
1赞 · 0采集
-
-
寒杨
2015-08-19
- 将老师间的人物跟着鼠标旋转的代码整理了下:
// The angle between dirA and dirB around axis(向量a,b以轴axis的夹角)
static float AngleAroundAxis (Vector3 dirA, Vector3 dirB, Vector3 axis)
{
// Project A and B onto the plane orthogonal target axis
dirA = dirA - Vector3.Project (dirA, axis);//dirA到轴axis的投影(计算dirA投影在垂直于axis轴的平面的向量)
dirB = dirB - Vector3.Project (dirB, axis);//dirB到轴axis的投影(计算dirB投影在垂直于axis轴的平面的向量
// Find (positive) angle between A and B
float angle = Vector3.Angle (dirA, dirB);//得到鼠标前后两次位置在平面上的转动的角度
// Return angle multiplied with 1 or -1
return angle * (Vector3.Dot (axis, Vector3.Cross (dirA, dirB)) < 0 ? -1 : 1);//放回的值随夹角的增大而减小
}
-
2赞 · 1采集