Search Unity

How do I create 3 raycast for my character

Discussion in 'Physics' started by miguelvvplay321, Jul 24, 2021.

  1. miguelvvplay321

    miguelvvplay321

    Joined:
    Jul 14, 2021
    Posts:
    2
    hello i am a newbie in programming and to polish my character i want to make 3 raycast but i have searched for forums tutorials and have not found how to do it this is my code please help.
    currently the character has only one raycast.
    if the raycast code needs to be changed, please let me know.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Heromovement : MonoBehaviour
    6. {
    7.     //public
    8.     public float Speed;
    9.     public SpriteRenderer spriteRenderer;
    10.     public BoxCollider2D boxCollider2D;
    11.     public LayerMask floor;
    12.     public float Jumpforce;
    13.     //private
    14.     private Rigidbody2D Rigidbody2D;
    15.     private bool Grounded;
    16.  
    17.     void Start()
    18.     {
    19.         Rigidbody2D = GetComponent<Rigidbody2D>();
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         if (Input.GetKeyDown(KeyCode.W))
    25.         {
    26.             if (Grounded)
    27.             {
    28.                 jump();
    29.                 DobleJump = true;
    30.             }
    31.             else if (DobleJump)
    32.             {            
    33.                 jump();
    34.                 DobleJump = false;
    35.             }
    36.             else
    37.             {
    38.                     jump();
    39.                     DobleJump = true;
    40.             }
    41.  
    42.         }
    43.     }
    44.     void FixedUpdate()
    45.     {
    46.         if (Input.GetKey("d"))
    47.         {
    48.             Rigidbody2D.velocity = new Vector2(Speed, Rigidbody2D.velocity.y);
    49.             spriteRenderer.flipX = false;
    50.         }
    51.         else if (Input.GetKey("a"))
    52.         {
    53.             Rigidbody2D.velocity = new Vector2(-Speed, Rigidbody2D.velocity.y);
    54.             spriteRenderer.flipX = true;
    55.         }
    56.         else
    57.         {
    58.             Rigidbody2D.velocity = new Vector2(0, Rigidbody2D.velocity.y);
    59.         }
    60.         Raycast();
    61.     }
    62.     private void jump()
    63.     {
    64.         Rigidbody2D.AddForce(Vector2.up * Jumpforce);
    65.         if (DobleJump == true)
    66.         {
    67.             Rigidbody2D.velocity = Vector2.zero;
    68.         }
    69.     }
    70.     private void Raycast()
    71.     {
    72.  
    73.  
    74.         Debug.DrawRay(transform.position, Vector2.down * 0.25f, Color.red);
    75.         if (Physics2D.Raycast(boxCollider2D.bounds.center, Vector2.down, boxCollider2D.bounds.extents.y + 0.25f, floor))
    76.         {
    77.             Grounded = true;
    78.         }
    79.         else Grounded = false;
    80.     }
    81.  
    82. }