Unity C#当玩家转身时如何翻转手中的物品
我对unity还很陌生,但我有一个基本的FPS游戏,当我拿着枪的时候,我想让它在你的玩家转动时,手中的物品旋转以显示转动。例如,在播放《使命召唤》时,旋转角色时枪会旋转。这是我的代码,但它不工作Unity C#当玩家转身时如何翻转手中的物品,c#,rotation,unity3d,quaternions,C#,Rotation,Unity3d,Quaternions,我对unity还很陌生,但我有一个基本的FPS游戏,当我拿着枪的时候,我想让它在你的玩家转动时,手中的物品旋转以显示转动。例如,在播放《使命召唤》时,旋转角色时枪会旋转。这是我的代码,但它不工作 void Update(){ this.rotateEquppedOnTurn(); } private void rotateEquppedOnTurn(){ if(this.equippedItem != null){ InteractEquppableIte
void Update(){
this.rotateEquppedOnTurn();
}
private void rotateEquppedOnTurn(){
if(this.equippedItem != null){
InteractEquppableItem equip = this.equippedItem.gameObject.GetComponent<Interaction>() as InteractEquppableItem;
if(equip.rotatesWhenTurn){
float rotX = Input.GetAxis("Mouse X");
float rotY = Input.GetAxis("Mouse Y");
Quaternion tempRot = new Quaternion();
Quaternion tempCam = GameObject.Find("PlayerCamera").transform.rotation;
tempRot.x = tempCam.x + rotX;
tempRot.y = tempCam.y + rotY;
tempRot.z = tempCam.z;
this.equippedItem.gameObject.transform.rotation = tempRot;
}
}
}
void Update(){
这个。rotateEquppedOnTurn();
}
私有void rotateEquppedOnTurn(){
如果(this.equipItem!=null){
InteractEqualPableItem equip=this.equipItem.gameObject.GetComponent()作为InteractEqualPableItem;
如果(转动时装备旋转){
float rotX=Input.GetAxis(“鼠标X”);
float rotY=Input.GetAxis(“鼠标Y”);
四元数tempRot=新四元数();
四元数tempCam=GameObject.Find(“PlayerCamera”).transform.rotation;
tempRot.x=tempCam.x+rotX;
tempRot.y=tempCam.y+rotY;
tempRot.z=tempCam.z;
this.equipeditem.gameObject.transform.rotation=tempRot;
}
}
}
当使用此代码旋转角色时,枪只是以一种奇怪的方式旋转,这与我从旋转脚本中预期的不太一样
不要像这样直接指定旋转
this.equippedItem.gameObject.transform.rotation = tempRot;
用这样的东西来代替它
这个.equipItem.gameObject.transform.Rotate(新向量3(x,y,z))
可以使用鼠标运动导出x、y、z