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

What wrong with this?

Discussion in 'Scripting' started by Deleted User, Nov 23, 2014.

  1. Deleted User

    Deleted User

    Guest

    My 2d character controller works on horizontal movement when I stop on a slope it slides back down..
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class movementScript : MonoBehaviour {
    5.  
    6.     public float maxSpeed = 10f;
    7.     bool facingRight = true;
    8.  
    9.     Animator anim;
    10.     public AudioClip jump;
    11.  
    12.     bool grounded = false;
    13.     public Transform groundCheck;
    14.     float groundRadius = 0.1f;
    15.     public LayerMask whatIsGround;
    16.     public float jumpForce = 700f;
    17.  
    18.     void Start () {
    19.         anim = GetComponent<Animator>();
    20.     }
    21.  
    22.     void FixedUpdate () {
    23.  
    24.         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    25.         anim.SetBool ("Ground", grounded);
    26.  
    27.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    28.  
    29.         float move = Input.GetAxis("Horizontal");
    30.  
    31.         anim.SetFloat("Speed", Mathf.Abs (move));
    32.  
    33.         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
    34.  
    35.         if(move > 0 &&!facingRight)
    36.             Flip ();
    37.  
    38.         else if (move < 0 && facingRight)
    39.             Flip ();
    40.     }
    41.  
    42.  
    43.     void Update(){
    44.  
    45.         if (grounded && Input.GetKeyDown(KeyCode.UpArrow)){
    46.             anim.SetBool ("Ground", false);
    47.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    48.             if(Input.GetKeyDown(KeyCode.UpArrow)){
    49.                 if(!audio.isPlaying){
    50.                     audio.clip = jump;
    51.                     audio.Play ();
    52.                 }
    53.             }
    54.         }
    55.     }
    56.  
    57.     void Flip(){
    58.  
    59.         facingRight = !facingRight;
    60.         Vector3 theScale = transform.localScale;
    61.         theScale.x *= -1;
    62.         transform.localScale = theScale;
    63.     }
    64. }
     
  2. federicopintaluba

    federicopintaluba

    Joined:
    Oct 19, 2014
    Posts:
    13
    Did you try creating a Physics Material 2D with 1 on Friction?
     
  3. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    It goes back down because gravity is pushing it down. Setting the friction could definitely help.
     
  4. Deleted User

    Deleted User

    Guest

    I did but it goes down by a little and it gets stuck going up the slope
     
  5. Deleted User

    Deleted User

    Guest

    it gets stuck going up the slope

    This is how my slope looks
     
    Last edited by a moderator: Nov 24, 2014
  6. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    What you could do is make your character not affected by gravity once you hit the ground, and then once you jump, turn it back on.