Unity3d charactercontroller在跳跃时在平台上弹跳,但在下落时不弹跳。

huangapple go评论78阅读模式
英文:

Unity3d charactercontroller bouncing on platforms while jumping but not when falling

问题

以下是你提供的代码的翻译:

  1. 我在我的游戏中遇到了一些奇怪的问题。我使用3D模型和一个固定的摄像机来制作2D平台游戏。我有一个角色控制器,带有以下用于移动和跳跃的脚本:
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class PlayerMovement : MonoBehaviour
  7. {
  8. private CharacterController controller;
  9. private Vector3 playerVelocity;
  10. private bool groundedPlayer;
  11. [SerializeField] private Animator _animator;
  12. [SerializeField] private float playerSpeed = 2.0f;
  13. [SerializeField] private float jumpHeight = 4.0f;
  14. [SerializeField] private float gravityValue = -1f;
  15. [SerializeField] private AudioSource _audioSource;
  16. [SerializeField] private AudioClip _stepStone;
  17. private void Start()
  18. {
  19. controller = GetComponent<CharacterController>();
  20. _animator = GetComponent<Animator>();
  21. _audioSource = GetComponent<AudioSource>();
  22. }
  23. void Update()
  24. {
  25. MovePlayer();
  26. }
  27. private void MovePlayer()
  28. {
  29. float x = Input.GetAxis("Horizontal");
  30. groundedPlayer = controller.isGrounded;
  31. if (groundedPlayer && playerVelocity.y < 0)
  32. {
  33. playerVelocity.y = 0f;
  34. _animator.SetBool("Jumping", false);
  35. }
  36. Vector3 move = new Vector3(x, 0, 0);
  37. controller.Move(move * Time.deltaTime * playerSpeed);
  38. if (move != Vector3 zero)
  39. {
  40. gameObject.transform.forward = move;
  41. }
  42. // 改变玩家的高度位置
  43. if (Input.GetButtonDown("Jump") && groundedPlayer)
  44. {
  45. _animator.SetBool("Jumping", true);
  46. playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
  47. }
  48. playerVelocity.y += gravityValue * Time.deltaTime;
  49. controller.Move(playerVelocity * Time.deltaTime);
  50. if (groundedPlayer)
  51. {
  52. _animator.SetInteger("Speed", Convert.ToInt32(x));
  53. }
  54. }
  55. public void playStoneStep()
  56. {
  57. _audioSource.PlayOneShot(_stepStone);
  58. }
  59. }

关于你的角色在跳跃到平台上时弹跳,但在落下到平台上时不弹跳的问题,你已经尝试了一个不具有弹性的材质。你可以尝试以下方法来解决这个问题:

  1. 检查碰撞器:确保你的角色碰撞器配置正确,以便在与平台接触时触发弹跳。

  2. 物理材质:确保你的角色和平台之间的物理材质配置正确。你可以尝试调整摩擦力和弹性属性以获得所需的行为。

  3. 检查代码:检查你的脚本,确保在跳跃时适当地应用了力和速度。

如果问题仍然存在,你可能需要提供更多关于你的游戏和角色控制器设置的信息,以便更详细地分析问题。

英文:

I have something strange happening in my game.I use 3d models and a fixed camera to make a 2D platformer. I have a charactercontroller with the following script for movement and jumping:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class PlayerMovement : MonoBehaviour
  6. {
  7. private CharacterController controller;
  8. private Vector3 playerVelocity;
  9. private bool groundedPlayer;
  10. [SerializeField] private Animator _animator;
  11. [SerializeField] private float playerSpeed = 2.0f;
  12. [SerializeField] private float jumpHeight = 4.0f;
  13. [SerializeField] private float gravityValue = -1f;
  14. [SerializeField] private AudioSource _audioSource;
  15. [SerializeField] private AudioClip _stepStone;
  16. private void Start()
  17. {
  18. controller = GetComponent&lt;CharacterController&gt;();
  19. _animator = GetComponent&lt;Animator&gt;();
  20. _audioSource = GetComponent&lt;AudioSource&gt;();
  21. }
  22. void Update()
  23. {
  24. MovePlayer();
  25. }
  26. private void MovePlayer()
  27. {
  28. float x = Input.GetAxis(&quot;Horizontal&quot;);
  29. groundedPlayer = controller.isGrounded;
  30. if (groundedPlayer &amp;&amp; playerVelocity.y &lt; 0)
  31. {
  32. playerVelocity.y = 0f;
  33. _animator.SetBool(&quot;Jumping&quot;, false);
  34. }
  35. Vector3 move = new Vector3(x, 0,0);
  36. controller.Move(move * Time.deltaTime * playerSpeed);
  37. if (move != Vector3.zero)
  38. {
  39. gameObject.transform.forward = move;
  40. }
  41. // Changes the height position of the player..
  42. if (Input.GetButtonDown(&quot;Jump&quot;) &amp;&amp; groundedPlayer)
  43. {
  44. _animator.SetBool(&quot;Jumping&quot;, true);
  45. playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
  46. }
  47. playerVelocity.y += gravityValue * Time.deltaTime;
  48. controller.Move(playerVelocity * Time.deltaTime);
  49. if (groundedPlayer)
  50. {
  51. _animator.SetInteger(&quot;Speed&quot;, Convert.ToInt32(x));
  52. }
  53. }
  54. public void playStoneStep()
  55. {
  56. _audioSource.PlayOneShot(_stepStone);
  57. }
  58. }

My character bounces if it jumps on platforms but not when falling on them. I tried a non bouncy material. how can I fix this?
Here is a video showing it : <https://youtu.be/ZhV8nHklsH8>

答案1

得分: 1

以下是您要翻译的内容:

  1. 我会通过添加一个检查角色是否着陆在地面上的功能来实现这一点。我已经将您的功能拆分成不同的、更易管理的部分。我添加了一个层遮罩,您需要配置它以匹配地面的层。
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class PlayerMovement : MonoBehaviour
  7. {
  8. private CharacterController controller;
  9. private Vector3 playerVelocity;
  10. private bool groundedPlayer;
  11. [SerializeField] private Animator _animator;
  12. [SerializeField] private float playerSpeed = 2.0f;
  13. [SerializeField] private float jumpHeight = 4.0f;
  14. [SerializeField] private float gravityValue = -1f;
  15. [SerializeField] private LayerMask groundMask;
  16. [SerializeField] private AudioSource _audioSource;
  17. [SerializeField] private AudioClip _stepStone;
  18. private void Start()
  19. {
  20. controller = GetComponent<CharacterController>();
  21. _animator = GetComponent<Animator>();
  22. _audioSource = GetComponent<AudioSource>();
  23. }
  24. void Update()
  25. {
  26. HandleMovement();
  27. HandleJump();
  28. HandleGravity();
  29. }
  30. private void HandleMovement()
  31. {
  32. controller.Move(new Vector3(Input.GetAxis("Horizontal"), 0, 0) * Time.deltaTime * playerSpeed);
  33. if (groundedPlayer)
  34. {
  35. _animator.SetInteger("Speed", Convert.ToInt32(x));
  36. }
  37. }
  38. private void HandleJump()
  39. {
  40. if (Input.GetButtonDown("Jump") && groundedPlayer)
  41. {
  42. _animator.SetBool("Jumping", true);
  43. playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
  44. }
  45. }
  46. private void HandleGravity()
  47. {
  48. playerVelocity.y += gravityValue * Time.deltaTime;
  49. controller.Move(playerVelocity * Time.deltaTime);
  50. }
  51. void OnCollisionEnter(Collision col)
  52. {
  53. if (IsInLayerMask(col.gameObject, groundMask))
  54. {
  55. playerVelocity.y = 0f;
  56. _animator.SetBool("Jumping", false);
  57. }
  58. }
  59. private bool IsInLayerMask(GameObject obj, LayerMask layerMask)
  60. {
  61. return ((layerMask.value & (1 << obj.layer)) > 0);
  62. }
  63. public void playStoneStep()
  64. {
  65. _audioSource.PlayOneShot(_stepStone);
  66. }
  67. }

请注意,我已将代码部分保留为原文。

英文:

I would do this by adding something that checks if you land on the floor. I did below split your functionality into different, more manageable parts. I've added a layer mask which you will need to configure with the layer of the ground.

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class PlayerMovement: MonoBehaviour
  6. {
  7. private CharacterController controller;
  8. private Vector3 playerVelocity;
  9. private bool groundedPlayer;
  10. [SerializeField] private Animator _animator;
  11. [SerializeField] private float playerSpeed = 2.0f;
  12. [SerializeField] private float jumpHeight = 4.0f;
  13. [SerializeField] private float gravityValue = -1f;
  14. [SerializeField] privateLayerMask groundMask;
  15. [SerializeField] private AudioSource _audioSource;
  16. [SerializeField] private AudioClip _stepStone;
  17. private void Start()
  18. {
  19. controller = GetComponent&lt;CharacterController&gt;();
  20. _animator = GetComponent&lt;Animator&gt;();
  21. _audioSource = GetComponent&lt;AudioSource&gt;();
  22. }
  23. void Update()
  24. {
  25. HandleMovement();
  26. HandleJump();
  27. HandleGravity();
  28. }
  29. private void HandleMovement()
  30. {
  31. controller.Move(new Vector3(Input.GetAxis(&quot;Horizontal&quot;), 0,0) * Time.deltaTime * playerSpeed);
  32. if (groundedPlayer)
  33. {
  34. _animator.SetInteger(&quot;Speed&quot;, Convert.ToInt32(x));
  35. }
  36. }
  37. private void HandleJump()
  38. {
  39. if (Input.GetButtonDown(&quot;Jump&quot;) &amp;&amp; groundedPlayer)
  40. {
  41. _animator.SetBool(&quot;Jumping&quot;, true);
  42. playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
  43. }
  44. }
  45. private void HandleGravity()
  46. {
  47. playerVelocity.y += gravityValue * Time.deltaTime;
  48. controller.Move(playerVelocity * Time.deltaTime);
  49. }
  50. void OnCollisionEnter(Collision col)
  51. {
  52. if (IsInLayerMask(col.gameObject, groundMask))
  53. {
  54. playerVelocity.y = 0f;
  55. _animator.SetBool(&quot;Jumping&quot;, false);
  56. }
  57. }
  58. private bool IsInLayerMask(GameObject obj, LayerMask layerMask)
  59. {
  60. return ((layerMask.value &amp; (1 &lt;&lt; obj.layer)) &gt; 0);
  61. }
  62. public void playStoneStep()
  63. {
  64. _audioSource.PlayOneShot(_stepStone);
  65. }
  66. }

huangapple
  • 本文由 发表于 2023年2月9日 03:08:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定