我正在制作一個帶有重力切換機制的 3d 游戲。我讓玩家在地圖上走動,它適用于所有角度,但是跳躍是一團糟。我想知道如何才能使跳躍起作用。
跳轉當前只是向本地向上向量添加一個值。我不知道如何解決它。我曾經讓它作業過,但后來我不得不切換到物理運動而不是靜態運動,現在它不起作用。
這是代碼:
void Update()
{
//think about moving
isRunning = (Input.GetKey(KeyCode.LeftControl));
isShifting = IntToBool((int)((BoolToInt(Input.GetKey(KeyCode.LeftAlt)))*BoolToInt(!isShiftable)) (BoolToInt(Input.GetKey(KeyCode.LeftShift))*BoolToInt(isShiftable)));
Vector3 forward = transform.forward;//transform.TransformDirection(Vector3.forward);
Vector3 right = transform.right;//transform.TransformDirection(Vector3.right);
if (((isShifting && isRunning)||!(isShifting && isRunning))) {
curSpeedX = walkingSpeed*Input.GetAxis("Vertical");
curSpeedY = walkingSpeed*Input.GetAxis("Horizontal");
}
if (isShifting) {
curSpeedX = ShiftSpeed*Input.GetAxis("Vertical");
curSpeedY = ShiftSpeed*Input.GetAxis("Horizontal");
}
if (isRunning) {
curSpeedX = runningSpeed*Input.GetAxis("Vertical");
curSpeedY = runningSpeed*Input.GetAxis("Horizontal");
}
//jump
if (!isGrounded) {
ypoa -= gravity;
}
if (Input.GetKeyDown(KeyCode.Space)) {
ypoa = jumpheight;
}
//actualy move
moveDir = (ypoa*transform.up) (curSpeedX*transform.forward) (curSpeedY*transform.right);
rb.velocity = (moveDir*speed);
moveDir = new Vector3(0,0,0);
}
void OnCollisionEnter() {
jumcount = 0;
ypoa = 0;
isGrounded = true;
}
void OnCollisionExit() {
isGrounded = false;
}
void OnCollisionStay() {
isGrounded = true;
}
我正在使用 unity 2019.4.28f 并且在 Windows 10 上
幫助表示贊賞。
uj5u.com熱心網友回復:
嘗試將您的物理代碼移動到 FixedUpdate 中。如果這不起作用,請您詳細說明一下,例如按空格時會發生什么?
void Update()
{
//think about moving
isRunning = (Input.GetKey(KeyCode.LeftControl));
isShifting = IntToBool((int)((BoolToInt(Input.GetKey(KeyCode.LeftAlt)))*BoolToInt(!isShiftable)) (BoolToInt(Input.GetKey(KeyCode.LeftShift))*BoolToInt(isShiftable)));
Vector3 forward = transform.forward;//transform.TransformDirection(Vector3.forward);
Vector3 right = transform.right;//transform.TransformDirection(Vector3.right);
if (((isShifting && isRunning)||!(isShifting && isRunning))) {
curSpeedX = walkingSpeed*Input.GetAxis("Vertical");
curSpeedY = walkingSpeed*Input.GetAxis("Horizontal");
}
if (isShifting) {
curSpeedX = ShiftSpeed*Input.GetAxis("Vertical");
curSpeedY = ShiftSpeed*Input.GetAxis("Horizontal");
}
if (isRunning) {
curSpeedX = runningSpeed*Input.GetAxis("Vertical");
curSpeedY = runningSpeed*Input.GetAxis("Horizontal");
}
//jump
if (!isGrounded) {
ypoa -= gravity;
}
if (Input.GetKeyDown(KeyCode.Space)) {
ypoa = jumpheight;
}
//actualy move
moveDir = (ypoa*transform.up) (curSpeedX*transform.forward) (curSpeedY*transform.right);
rb.velocity = (moveDir*speed);
moveDir = new Vector3(0,0,0);
}
void OnCollisionEnter() {
jumcount = 0;
ypoa = 0;
isGrounded = true;
}
void OnCollisionExit() {
isGrounded = false;
}
void OnCollisionStay() {
isGrounded = true;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/372149.html
