Search Unity

Should I scrap my 2D Character Controller because of how I shortsightedly started to build it?

Discussion in 'Scripting' started by noahl_unity, Feb 17, 2020.

  1. noahl_unity

    noahl_unity

    Joined:
    Feb 17, 2020
    Posts:
    5
    Hello! I've been trying to create the framework for a game for the past couple of days and came across one of the first major hurdles... slopes. I've been stumped on how to tackle them for a few hours now, and I debating starting all over in my script but this time dealing with raycasts instead of just creating new vector2's and changing the physics. Is that a good idea? Can someone help me figure out the logical next step to tackling slopes in my script?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public float moveSpeed = 400f;
    8.     public float jumpVelocity;
    9.     public float lowJumpMult = 2f;
    10.     public float fallMult = 2.5f;
    11.     public float airResist = 0f;
    12.  
    13.     Rigidbody2D rb;
    14.  
    15.     private bool isGrounded;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         rb = GetComponent<Rigidbody2D>();
    20.         isGrounded = false; //to check if the player is on the ground so that they can't jump twice
    21.     }
    22.  
    23.     private void OnTriggerEnter2D(Collider2D collider)
    24.     {
    25.         if (collider.gameObject.tag == ("Terrain"))
    26.         {
    27.             isGrounded = true;
    28.         }
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     void Update()
    33.     {
    34.         if (Input.GetKey(KeyCode.RightArrow)) //moving right
    35.         {
    36.             if (isGrounded)
    37.             {
    38.                 rb.velocity = new Vector2 (moveSpeed * Time.deltaTime, rb.velocity.y);
    39.             }
    40.             else
    41.             {
    42.                 rb.velocity =  new Vector2 (moveSpeed * Time.deltaTime * airResist, rb.velocity.y);
    43.             }
    44.         }
    45.  
    46.         if (Input.GetKey(KeyCode.LeftArrow)) //moving left
    47.         {
    48.             if (isGrounded)
    49.             {
    50.                 rb.velocity = new Vector2(moveSpeed * Time.deltaTime * -1, rb.velocity.y);
    51.             }
    52.             else
    53.             {
    54.                 rb.velocity = new Vector2(moveSpeed * Time.deltaTime * airResist * -1, rb.velocity.y);
    55.             }
    56.         }
    57.  
    58.  
    59.         if (rb.velocity.x >= 0 && !Input.GetKey(KeyCode.RightArrow)) //stop moving on the x axis when the right key is released
    60.         {
    61.             rb.velocity = new Vector2(0 * Time.deltaTime, rb.velocity.y);
    62.         }
    63.  
    64.         if (rb.velocity.x <= 0 && !Input.GetKey(KeyCode.LeftArrow)) //stop moving on the x axis when the left key is released
    65.         {
    66.             rb.velocity = new Vector2(0 * Time.deltaTime, rb.velocity.y);
    67.         }
    68.  
    69.         if (Input.GetButtonDown("Jump") && isGrounded == true) //jumping
    70.         {
    71.             rb.velocity = (Vector2.up * jumpVelocity);
    72.             isGrounded = false; //making sure that when ur in the air you cannot jump a second time
    73.         }
    74.  
    75.         if (rb.velocity.y < 0)
    76.         {
    77.             rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMult - 1) * Time.deltaTime; //falling faster for weighty physics
    78.         }
    79.         else if (rb.velocity.y > 0 && !Input.GetButton("Jump"))
    80.         {
    81.             rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMult - 1) * Time.deltaTime; //dynamic jump (when key is released gravity is increased)
    82.         }
    83.  
    84.     }
    85.  
    86. }
    87.  
    This is what my code looks like and I don't know if I went about it the right way or not. Thanks in advance to anyone brave enough to help me!