- Home /
 
               Question by 
               unity_580000564 · Oct 27, 2021 at 07:08 PM · 
                scripting problemraycast2d-platformerscripting beginnerjumping  
              
 
              Why is my raycast projecting from the middle instead of the bottom?
I'm making a 2d platformer for school and I found this script for jumping but for some reason when I use it my raycast doesn't from the bottom.

public class Player : MonoBehaviour { private Rigidbody2D rb; private bool CanJump = true; [SerializeField] private float MovementSpeed; [SerializeField] private float JumpSpeed;
 [SerializeField] private float RayLength;
 [SerializeField] private float RayPositionOffset;
 Vector3 RayPositionCenter;
 Vector3 RayPositionLeft;
 Vector3 RayPositionRight;
 RaycastHit2D[] GroundHitsCenter;
 RaycastHit2D[] GroundHitsLeft;
 RaycastHit2D[] GroundHitsRight;
 RaycastHit2D[][] AllRaycastHits = new RaycastHit2D[3][];
 private void Awake()
 {
     rb = GetComponent<Rigidbody2D>();
 }
 private void Update()
 {
     Movement();
     Jump();
 }
 private void Jump()
 {
     RayPositionCenter = transform.position + new Vector3(0, RayLength * .5f, 0);
     RayPositionLeft = transform.position + new Vector3(-RayPositionOffset, RayLength * .5f, 0);
     RayPositionRight = transform.position + new Vector3(RayPositionOffset, RayLength * .5f, 0);
     GroundHitsCenter = Physics2D.RaycastAll(RayPositionCenter, -Vector2.up, RayLength);
     GroundHitsLeft = Physics2D.RaycastAll(RayPositionLeft, -Vector2.up, RayLength);
     GroundHitsRight = Physics2D.RaycastAll(RayPositionRight, -Vector2.up, RayLength);
     AllRaycastHits[0] = GroundHitsCenter;
     AllRaycastHits[1] = GroundHitsLeft;
     AllRaycastHits[2] = GroundHitsRight;
     CanJump = GroundCheck(AllRaycastHits);
     Debug.DrawRay(RayPositionCenter, -Vector2.up * RayLength, Color.red);
     Debug.DrawRay(RayPositionLeft, -Vector2.up * RayLength, Color.red);
     Debug.DrawRay(RayPositionRight, -Vector2.up * RayLength, Color.red);
 }
 private bool GroundCheck(RaycastHit2D[][] GroundHits)
 {
     foreach (RaycastHit2D[] HitList in GroundHits)
     {
         foreach (RaycastHit2D hit in HitList)
         {
             if (hit.collider != null)
             {
                 if (hit.collider.tag != "BoxCollider2D")
                 {
                     return true;
                 }
             }
         }
     }
     return false;
 }
 private void Movement()
 {
     if (Input.GetAxisRaw("Horizontal") > 0)
     {
         rb.velocity = new Vector2(MovementSpeed * Time.fixedDeltaTime, rb.velocity.y);
     }
     else if (Input.GetAxisRaw("Horizontal") < 0)
     {
         rb.velocity = new Vector2(-MovementSpeed * Time.fixedDeltaTime, rb.velocity.y);
     }
     else
     {
         rb.velocity = new Vector2(0, rb.velocity.y);
     }
     if (Input.GetKey(KeyCode.Space) && CanJump)
     {
         rb.velocity = new Vector2(rb.velocity.x, 0);
         rb.velocity = new Vector2(rb.velocity.x, JumpSpeed);
     }
 }
}
 
                 
                screenshot-62.png 
                (8.5 kB) 
               
 
              
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
How do I make it so it spawns 1 clone instead of 2? 2 Answers
I need help with raycasts and with color of gameobject [SOLVED] 3 Answers
Can someone help me with the jumping in this Player Controller script? 1 Answer
setting a bool on another object's animator by player's raycast 2 Answers
I'm trying to allow my character to only jump when grounded 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                