【发布时间】:2018-08-07 03:57:35
【问题描述】:
现在我有一个附有动作脚本的第一人称角色。我使用刚体和角色控制器。相机控制父对象与玩家对象的转换,在本例中为胶囊。
玩家对象上面有移动脚本,相机上面有一个相机控制器,只允许鼠标控制玩家对象的变换。
当我跳跃时,我的脚本这样做是为了让玩家保持动量,并且不能在半空中停下来,但是如果我转动我的相机,玩家对象也会根据变换改变轨迹。如果我向右看,在半空中,我的跳跃也会跟随摄像机向右。我希望在半空中跳跃运动,保持原来的轨迹。有人可以帮我添加该功能,但相机仍然可以环顾四周吗?
我的动作脚本:
void movement () {
moveVector = Vector3.zero;
moveVector.x = Input.GetAxisRaw("Horizontal");
moveVector.z = Input.GetAxisRaw("Vertical");
if (controller.isGrounded) {
verticalVelocity = -1;
if (Input.GetButtonDown("Jump")) {
verticalVelocity = jumpforce;
}
} else {
verticalVelocity -= gravity * Time.deltaTime;
moveVector = lastMove;
}
moveVector.y = 0;
moveVector.Normalize ();
moveVector *= playerspeed;
moveVector.y = verticalVelocity;
worldMove = transform.TransformDirection (moveVector);
controller.Move (worldMove * Time.deltaTime);
//controller.Move (moveVector.z * transform.forward * Time.deltaTime);
//controller.Move (moveVector.x * transform.right * Time.deltaTime);
//controller.Move (moveVector.y * transform.up * Time.deltaTime);
//controller.Move (moveVector * Time.deltaTime);
lastMove = moveVector;
}
它是:
moveVector = lastMove;
这样做是为了让我的播放器不会停在半空中。
此外,如果可以做到这一点,那就太棒了,因为你不能在空中改变方向,但你可以稍微拖动轨迹,但要以一种平稳的方式,就像在反恐精英:全球攻势等中一样。
更新: 这是我的相机代码:
public class CameraController : MonoBehaviour {
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
GameObject character;
// Use this for initialization
void Start () {
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update () {
var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
//Låser kameraret så man ikke kan kigge længere ned eller op end 90 grader
mouseLook.y = Mathf.Clamp (mouseLook.y, -90f, 90f);
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
}
}
【问题讨论】:
标签: c# unity3d animation scripting