Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Highcarve · Jun 10, 2021 at 09:33 AM · 2d game2d-platformer2d spriteslogic

Sprite turning around repeatedly against a wall. How to have "Wallslide" flip sprite?

Hello! So I have been messing around with unity, trying to get better at programming. The issue I am currently experiencing is in regards to wall-sliding. Presently, I have my character move left when holding A (and his transform.localscale.x = 1) , and move to the right with D (and his transform.localscale.x = -1). These functions also work when in a jumping or falling state.

However, the issue I have been suffering from is wall-sliding. I am trying to have my character face to the right when clinging on a left wall, and vice versa, however, despite after testing several different methods, I can't seem to get it to work. What ends up happening is that it functions perfectly normal when on the ground/air, but the second I get against a wall, the transform.localscale.x shoots between 1 and -1, rapidly, causing my sprite to spazz out.

Here is my code thus far (This lacks anything in regards to the flip when wall-sliding due to me not being able to figure it out):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.InputSystem;
 
 public class PlayerMovement : MonoBehaviour
 {
     [SerializeField] float jumpHeight;
     [SerializeField] float runSpeed;
     private PlayerInputScript playerActionControls;
     private Rigidbody2D rb;
     private Animator ani;
     private Transform smTransform;
     private CapsuleCollider2D capCollider;
     private BoxCollider2D boxCollider;
     bool facingRight = true;
     bool wallSliding = false;
 
     private void Awake()
     {
         playerActionControls = new PlayerInputScript();
         rb = GetComponent<Rigidbody2D>();
         ani = GetComponent<Animator>();
         smTransform = GetComponent<Transform>();
         capCollider = GetComponent<CapsuleCollider2D>();
         boxCollider = GetComponent<BoxCollider2D>();
     }
 
     private void OnEnable()
     {
         playerActionControls.Enable();
     }
 
     private void OnDisable()
     {
         playerActionControls.Disable();
     }
 
     void Start()
     {
         playerActionControls.PlayerMovement.Jumping.performed += _ => Jumping();
     }
 
     void Update()
     {
         Running();
         JumpingAni();
         FallingAni();
         WallSlide();
     }
 
     private void Running()
     {
         float rlRunning = playerActionControls.PlayerMovement.Running.ReadValue<float>();
         Vector2 playerVelocity = new Vector2(rlRunning * runSpeed, rb.velocity.y);
         rb.velocity = playerVelocity;
 
         //Sets Animation
         if (Mathf.Abs(rlRunning) <= 0)
         {
             ani.SetBool("Running", false);
         }
         else
         {
             ani.SetBool("Running", true);
         }
 
 
 
         //Flips Sprite depending on movement
 
         if (rlRunning > 0 && !facingRight && !wallSliding)
         {
             FlipSprite();
         }
         else if (rlRunning < 0 && facingRight && !wallSliding)
         {
             FlipSprite();
         }
         
        
     }
 
 
     private void Jumping()
     {
         if (!boxCollider.IsTouchingLayers(LayerMask.GetMask("Foreground")))
         {
             return;
         }
         rb.AddForce(new Vector2(0, jumpHeight), ForceMode2D.Impulse);
     }
 
     private void JumpingAni()
     {
         if(rb.velocity.y >= .1)
         {
             ani.SetBool("Jumping", true);
             
         }
         if (rb.velocity.y <= 0)
         {
             ani.SetBool("Jumping", false);
             
         }
     }
 
     private void FallingAni()
     {
         if (rb.velocity.y <= -.1 && !capCollider.IsTouchingLayers(LayerMask.GetMask("Walls")))
         {
             ani.SetBool("Falling", true);
             
         }
         if (rb.velocity.y >= 0)
         {
             ani.SetBool("Falling", false);
             
         }
         if (boxCollider.IsTouchingLayers(LayerMask.GetMask("Foreground"))){
             ani.SetBool("Falling", false);
         }
     }
 
     private void WallSlide()
     {
         if(capCollider.IsTouchingLayers(LayerMask.GetMask("Walls")) && (!boxCollider.IsTouchingLayers(LayerMask.GetMask("Foreground")))){
             Vector2 wallslideVelocity = new Vector2(rb.velocity.x, .5f * rb.velocity.y);
             rb.velocity = wallslideVelocity;
             ani.SetBool("Wallsliding", true);
             wallSliding = true;
         }
         else
         {
             ani.SetBool("Wallsliding", false);
             wallSliding = false;
         }
     }
 
     private void FlipSprite()
     {
         facingRight = !facingRight;
 
         Vector2 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;      
     }
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

168 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

box collider 2d are not touching! please help!,box Collider 2d are not touching 0 Answers

How can I add parallax to background sprites which are being instantiated only in play mode. 1 Answer

How to sync animation if animation should be use with specially object 0 Answers

Instantiate a GameObject with a specific Z rotation 2 Answers

My 2D Player Can't Move (2d Photon Game) :(,My Player Don't Move (2d Character Controller 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges