#c# #unity3d #game-physics
Вопрос:
я создаю 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 amp;amp; isRunning)||!(isShifting amp;amp; 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.28 f и нахожусь в Windows 10
помощь приветствуется.
Ответ №1:
Попробуйте переместить свой код физики в 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 amp;amp; isRunning)||!(isShifting amp;amp; 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; }