r/UnityHelp • • Dec 15 '21

Fixing my movement script.

Hey unity community!

Im making a 2D platformer ( I know, very original) and my character moves and jumps around perfectly. My problem is that it keeps moving through walls.

  1. using System.Collections;

  2. using System.Collections.Generic;

  3. using UnityEngine;

  4. public class Playermove : MonoBehaviour

  5. {

  6. Animator playeranimator;

  7. Rigidbody2D playerRb;

  8. public float speed = 4f;

  9. bool facingRight;

  10. private bool isGrounded;

  11. public Transform feetpos;

  12. public float checkradius;

  13. public LayerMask whatisground;

  14. public float jumpforce;

  15. private float jumptimecounter;

  16. public float jumptime;

  17. private bool isJumping;

  18. // Start is called before the first frame update

  19. void Start()

  20. {

  21. playeranimator = GetComponent<Animator>();

  22. playerRb = GetComponent<Rigidbody2D>();

  23. }

  24. // Update is called once per frame

  25. void Update()

  26. {

  27. //get ground

  28. isGrounded = Physics2D.OverlapCircle(feetpos.position, checkradius, whatisground);

  29. //Get input

  30. float movement = Input.GetAxis("Horizontal");

  31. //Move along the x axis

  32. Vector3 playerPosition = transform.position;

  33. playerPosition.x = playerPosition.x + movement*speed *Time.deltaTime;

  34. transform.position = playerPosition;

  35. //Jump

  36. if(isGrounded == true && Input.GetKeyDown(KeyCode.Space)){

  37. isJumping = true;

  38. jumptimecounter = jumptime;

  39. playerRb.velocity = Vector2.up * jumpforce;

  40. }

  41. if(Input.GetKey(KeyCode.Space) && isJumping){

  42. if(jumptimecounter > 0){

  43. playerRb.velocity = Vector2.up * jumpforce;

  44. jumptimecounter -= Time.deltaTime;

  45. }

  46. else{

  47. isJumping = false;

  48. }

  49. }

  50. if(Input.GetKeyUp(KeyCode.Space)){

  51. isJumping = false;

  52. }

  53. /*

  54. if(Input.GetKey(KeyCode.Space) )

  55. {

  56. playerRb.AddForce(Vector2.up*1f, ForceMode2D.Impulse);

  57. }

  58. */

  59. //prevent sliding down slopes

  60. if(Input.GetKey(KeyCode.Space) == false || Input.GetAxis("Horizontal") == 0){

  61. playerRb.constraints = RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;

  62. }

  63. else{

  64. playerRb.constraints = RigidbodyConstraints2D.FreezeRotation;

  65. }

  66. Flip(movement);

  67. //Change Animation

  68. playeranimator.SetFloat("Speed", speed*0.75f);

  69. playeranimator.SetFloat("Move", Mathf.Abs(movement));

  70. playeranimator.SetBool("Jump", isJumping);

  71. }

  72. void Flip(float m)

  73. {

  74. //Get current direction and compare it to movement direction

  75. if(facingRight && m < 0 || !facingRight && m > 0)

  76. {

  77. //if they are not the same, then flip and change facingdirection

  78. transform.Rotate(new Vector3(0,180,0));

  79. facingRight = !facingRight;

  80. }

  81. }

  82. }

Thats my movement script in its entirety. I wasnt sure how much of it was needed.

I think that this part is whats causing the problem.

  1. Vector3 playerPosition = transform.position;

  2. playerPosition.x = playerPosition.x + movement*speed *Time.deltaTime;

  3. transform.position = playerPosition;

I have tried replacing that part with various solutions using rigibodies but that just causes my character to slowly move downwards and not be able to move side to side.

Please help. Thank you.

3 Upvotes

9 comments sorted by

View all comments

1

u/Bonejob Code Guru Dec 15 '21

ok so show us the code for your collision detection. Are you using standard layer-based collision for wall checks?

https://gamedevelopertips.com/unity-collision-detection-2d/

1

u/IAmATicTacAddict Dec 15 '21

I have Colliders on both the ground and player. Thats it.

1

u/Bonejob Code Guru Dec 16 '21

are you using the trigger from the collider on the ground layer, and checking if its the player or are you doing the collision on the player object?

1

u/IAmATicTacAddict Dec 17 '21

The player has a Collider and the ground has a tilemap Collider. Thats it.

1

u/Bonejob Code Guru Dec 17 '21

go watch the video man, I don't know if this is a language problem or an understanding problem but you did not answer how the collisions trigger was rigged or provide context.

1

u/IAmATicTacAddict Dec 17 '21

What video??? I did answer, i dont have anything rigged. I added Collider Components to My player sprite and the ground, thats it.

1

u/Bonejob Code Guru Dec 17 '21

My bad on the video I thought I had linked one earlier, but I must have it confused with another post. Here is the video I was referring to

https://www.youtube.com/watch?v=ZoZcBgRR9ns

and the question was "are you using the trigger from the collider on the ground layer"?

further;

https://gamedevelopertips.com/unity-collision-detection-2d/

1

u/IAmATicTacAddict Dec 17 '21

Thanks, turns out it wasnt really needed?? I added an overlapbox that does some stuff that prevents me from walking through walls. Thanks again tho.

1

u/Bonejob Code Guru Dec 18 '21

We do the best we can :)