Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Not getting full jump height on slope or edges. (2d)

Discussion in 'Scripting' started by Hideyoshi_K, Jul 24, 2014.

  1. Hideyoshi_K

    Hideyoshi_K

    Joined:
    Jun 15, 2014
    Posts:
    4
    So I've been having problems with trying to move my character on a slope. First I ran into the problem of the character lightly jumping and landing when walking down a slope instead of following the ground. To counter this I tried adding a downward force while on a slope to keep the character grounded, which worked in that regard but also had the effect of limiting the height of the character's jump while on a slope or edge of a platform. It's not consistent in how much it's reduced by either; sometimes the jump is just slightly lowered and other times the character can barely get off the ground.

    Here's the script I'm using for movement. The lines in of particular note are 136-154, but the rest of it might help clarify what I'm doing.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Moving : MonoBehaviour {
    5.    
    6.     public Vector2 jump = new Vector2 (0, 1);
    7.     public Vector3 movespeed = new Vector3 (1, 0, 0);
    8.     public Vector3 moveleft = new Vector3 (-1, 0, 0);
    9.    
    10.     public Transform toprightstart, toprightend, bottomrightstart, bottomrightend;
    11.     public Transform topleftstart, topleftend, bottomleftstart, bottomleftend;
    12.     public Transform lgroundstart, lgroundend, rgroundstart, rgroundend;
    13.  
    14.     public bool wall = false;
    15.     public bool onground = true;
    16.     public bool isattacking = false;
    17.     public bool facing_left = false;
    18.     public bool oldswitch = false;
    19.    
    20.     public float firerate = 0.5f;
    21.     private float nextfire = 0.0f;
    22.     public float attackmotion = 0.0f;
    23.    
    24.     public GameObject Attacking;
    25.    
    26.     Animator anim;
    27.    
    28.    
    29.     // Use this for initialization
    30.     void Start () {
    31.        
    32.         anim = GetComponent<Animator>();
    33.         Attacking.gameObject.SetActive (false);
    34.     }
    35.    
    36.     // Update is called once per frame
    37.     void Update () {
    38.         Movements ();
    39.         Raycasting ();
    40.         Attack ();
    41.         Flip_animation ();
    42.         slope ();
    43.     }
    44.     void Movements () {
    45.        
    46.         if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.LeftArrow)) {
    47.             anim.SetBool ("moving", true);
    48.         }
    49.         else {
    50.             anim.SetBool ("moving", false);
    51.         }
    52.        
    53.         if (Input.GetKey (KeyCode.RightArrow) && wall == false && isattacking == false) {
    54.             transform.Translate(movespeed * Time.deltaTime);
    55.         }
    56.         if (Input.GetKey (KeyCode.LeftArrow) && wall == false && isattacking == false) {
    57.             transform.Translate(-movespeed * Time.deltaTime);
    58.         }
    59.        
    60.        
    61.         if (Input.GetKeyDown (KeyCode.UpArrow) && onground == true) {
    62.             rigidbody2D.AddForce (jump);
    63.             anim.SetTrigger ("Jump");
    64.         }
    65.         if (Input.GetKeyUp (KeyCode.RightArrow)) {
    66.             transform.Translate (0,0,0);
    67.         }
    68.         if (Input.GetKeyUp (KeyCode.LeftArrow)) {
    69.             transform.Translate (0,0,0);
    70.         }
    71.     }
    72.     void Raycasting () {
    73.  
    74.         Debug.DrawLine (lgroundstart.position, lgroundend.position);
    75.        
    76.         if (Physics2D.Linecast (toprightstart.position, toprightend.position, 1 << LayerMask.NameToLayer ("Ground")) || (Physics2D.Linecast (bottomrightstart.position, bottomrightend.position, 1 << LayerMask.NameToLayer ("Ground")))) {
    77.             wall = true;
    78.         }
    79.         else     {wall = false;}
    80.  
    81.        
    82.         if (Physics2D.Linecast (lgroundstart.position, lgroundend.position, 1 << LayerMask.NameToLayer ("Ground")) || (Physics2D.Linecast (rgroundstart.position, rgroundend.position, 1 << LayerMask.NameToLayer ("Ground")))) {
    83.             onground = true;
    84.         }
    85.         else {onground = false;}
    86.  
    87.         if (onground == true) {
    88.             anim.SetBool ("On_Ground", true);
    89.         }
    90.         if (onground == false) {
    91.             anim.SetBool ("On_Ground", false);
    92.  
    93.         }
    94.     }
    95.  
    96.  
    97.     void Attack () {
    98.        
    99.        
    100.         if (Input.GetKeyDown(KeyCode.Space) && Time.time > nextfire){
    101.             Attacking.gameObject.SetActive (true);
    102.             isattacking = true;
    103.             nextfire = firerate + Time.time;
    104.             anim.SetBool ("Attacking", true);
    105.             Debug.Log ("Attacked");
    106.         }
    107.         if (Time.time > nextfire && isattacking == true){
    108.             Attacking.gameObject.SetActive (false);
    109.             isattacking = false;
    110.             anim.SetBool ("Attacking", false);
    111.         }
    112.     }
    113.    
    114.     void Flip_animation() {
    115.        
    116.         if (Input.GetKeyDown (KeyCode.LeftArrow)) {
    117.             facing_left = true;
    118.         }
    119.        
    120.         if (facing_left == true && facing_left == !oldswitch) {
    121.             transform.localScale = new Vector3 (-this.transform.localScale.x, this.transform.localScale.y, this.transform.localScale.z);
    122.             oldswitch = facing_left;
    123.             anim.SetTrigger ("Turning");
    124.         }
    125.        
    126.        
    127.         if (Input.GetKeyDown (KeyCode.RightArrow) && facing_left == true) {
    128.             transform.localScale = new Vector3 (-this.transform.localScale.x, this.transform.localScale.y, this.transform.localScale.z);
    129.             oldswitch = false;
    130.             facing_left = false;
    131.             anim.SetTrigger ("Turning");
    132.    
    133.        
    134.     }
    135. }
    136.     void slope() {
    137.         bool slope;
    138.         Vector2 Slope_Gravity = new Vector2 (0, -100);
    139.         if (Input.GetKeyDown (KeyCode.UpArrow)) {
    140.                 Slope_Gravity = new Vector2 (0,0);
    141.                 }
    142.         else {
    143.             Slope_Gravity = new Vector2 (0, -100);
    144.                 }
    145.  
    146.         if (Physics2D.Linecast (rgroundstart.position, rgroundend.position, 1 << LayerMask.NameToLayer ("Ground"))) {
    147.             slope = false;
    148.             }
    149.         else {
    150.             slope = true;
    151.             }
    152.         if (onground == true && slope == true) {
    153.             rigidbody2D.AddForce (Slope_Gravity);
    154.         }
    155.     }
    156. }
    157.