Unity3d 使角色面向其移动的方向?
我知道我应该把这个问题贴在Unity的问题页面上,但我确实贴了,而且已经三天没有收到答复了 所以我试图让角色面对它移动的方向,但我无法让它工作。我正在用暗黑破坏神风格的相机制作一个3d游戏,但不依赖鼠标,我使用的是《生存射手》的脚本作为参考 以下是动作脚本:Unity3d 使角色面向其移动的方向?,unity3d,rotation,character,Unity3d,Rotation,Character,我知道我应该把这个问题贴在Unity的问题页面上,但我确实贴了,而且已经三天没有收到答复了 所以我试图让角色面对它移动的方向,但我无法让它工作。我正在用暗黑破坏神风格的相机制作一个3d游戏,但不依赖鼠标,我使用的是《生存射手》的脚本作为参考 以下是动作脚本: public float speed = 6f; // The speed that the player will move at. public float rotateSpeed = 2f; public C
public float speed = 6f; // The speed that the player will move at.
public float rotateSpeed = 2f;
public Camera playerCam;
Vector3 movement; // The vector to store the direction of the player's movement.
Animator anim; // Reference to the animator component.
Rigidbody playerRigidbody; // Reference to the player's rigidbody.
int floorMask; // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
float camRayLength = 100f; // The length of the ray from the camera into the scene.
Vector3 rotationY;
void Awake ()
{
// Create a layer mask for the floor layer.
floorMask = LayerMask.GetMask ("Floor");
// Set up references.
anim = GetComponent <Animator> ();
playerRigidbody = GetComponent <Rigidbody> ();
}
void FixedUpdate ()
{
// Store the input axes.
float h = Input.GetAxisRaw ("Horizontal");
float v = Input.GetAxisRaw ("Vertical");
// Move the player around the scene.
Move (h, v);
// Turn the player to face the mouse cursor.
Turning (h, v);
}
void Move (float h, float v)
{
// Move the player to it's current position plus the movement.
Vector3 targetDirection = new Vector3(h, 0f, v);
targetDirection = Camera.main.transform.TransformDirection(targetDirection);
targetDirection = targetDirection.normalized * speed * Time.deltaTime;
targetDirection.y = 0.0f;
playerRigidbody.MovePosition (transform.position + targetDirection);
}
void Turning(float y, float x)
{
if (y != 0) {
rotationY.Set (0f, y, 0f);
//rotationY = rotationY.normalized * (5 * rotateSpeed);
Quaternion deltaRotation = Quaternion.Euler (rotationY);
playerRigidbody.MoveRotation (deltaRotation);
}
}
public float speed=6f;//玩家移动的速度。
公共浮动旋转速度=2f;
公共摄像机;
矢量3运动;//用于存储玩家移动方向的向量。
动画师动画;//对animator组件的引用。
刚体播放器刚体;//指运动员的刚体。
int floorMask;//一个图层蒙版,这样光线就可以投射到地板层上的游戏对象上。
浮动凸轮轴长度=100f;//从摄影机到场景的光线长度。
矢量3旋转;
无效唤醒()
{
//为地板层创建层遮罩。
floorMask=LayerMask.GetMask(“地板”);
//建立参考资料。
anim=GetComponent();
playerRigidbody=GetComponent();
}
无效固定更新()
{
//存储输入轴。
float h=Input.GetAxisRaw(“水平”);
float v=Input.GetAxisRaw(“垂直”);
//在场景中移动播放器。
移动(h,v);
//将播放机转向面向鼠标光标。
转弯(h,v);
}
无效移动(浮动h、浮动v)
{
//将玩家移动到当前位置加上移动。
矢量3目标方向=新矢量3(h,0f,v);
targetDirection=Camera.main.transform.TransformDirection(targetDirection);
targetDirection=targetDirection.normalized*速度*Time.deltaTime;
目标方向y=0.0f;
playerRigidbody.MovePosition(transform.position+targetDirection);
}
空心旋转(浮动y、浮动x)
{
如果(y!=0){
旋转y.设置(0f,y,0f);
//rotationY=rotationY.normalized*(5*rotateSpeed);
四元数三角旋转=四元数.欧拉(旋转Y);
playerRigidbody.MoveRotation(三角旋转);
}
}
但是,当我移动角色时,它总是面向屏幕的左下方。如何解决此问题?尝试
playerRigidbody.MoveRotation(playerRigidbody.rotation*deltaRotation)代码>。这不是我想做的。我希望它始终面向角色移动的方向。例如,如果我连续按D键,角色将始终面向屏幕右侧。然后无效旋转(float y,float x){playerRigidbody.rotation=Quaternion.LookRotation(new Vector3(y,0,x));}
应该做你想做的事情。但是我如何才能使角色平滑旋转?不知怎么说:无效旋转(float y,float x){if(y!=0 | | x!=0){var newRotation=Quaternion.LookRotation(new Vector3(y,0,x));var currentRotation=playerRigidbody.rotation;if(currentRotation!=newRotation)playerRigidbody.rotation=Quaternion.rotationwards(currentRotation,newRotation,rotateSpeed*Time.deltaTime)}