Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 anuj_jain · May 15, 2020 at 09:27 AM · flipflipping

,Why Character Is not Flipping ? There is some problem i am not able to see

using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine;

public class Player : MonoBehaviour { private Rigidbody2D _rigid; [SerializeField] private float _jumpforce = 5.0f; // variable for jumpforce [SerializeField] private bool _grounded = false; [SerializeField] private LayerMask _groundLayer; private bool resetjumpneeded = false; [SerializeField] private float _speed =5.0f; private Playeranimation _playeranim; private SpriteRenderer _playersprite;

        void Start()
 {
     _rigid = GetComponent<Rigidbody2D>();     // handle assign of rigidbody   
     _playeranim = GetComponent<Playeranimation>();  //handle assigned to _anim
     _playersprite   =  GetComponentInChildren<SpriteRenderer>(); 
 }

 // Update is called once per frame
 void Update()
 {
     Movement();
     checkgrounded();
       
 }

 void Movement()
 {

     float move = Input.GetAxisRaw("Horizontal");   //horizontal input for left /right movement

     if (move > 0 )
     {
         Flip(true);

     }

     else if (move < 0 )
     {
         Flip(false);
     }


     if (Input.GetKeyDown(KeyCode.Space) && _grounded == true)    //if space key && grounded == true
     {
         //jump 
         _rigid.velocity = new Vector2(_rigid.velocity.x, _jumpforce);
         _grounded = false;
         resetjumpneeded = true;
         StartCoroutine(resetjumpneededroutine());
         


     }
     
     _rigid.velocity = new Vector2(move* _speed, _rigid.velocity.y);  //current velocity = new velocity (current x ,jumpforce) 
     _playeranim.Move(move);
 }

 void checkgrounded()
 {

     RaycastHit2D hitinfo = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, 1 << 8);   //2dray cast to the ground 
     Debug.DrawRay(transform.position, Vector2.down * 0.6f, Color.green); //this is to show the raycast in game


     if (hitinfo.collider != null)
     {
         Debug.Log("Hit: " + hitinfo.collider.name);

         if (resetjumpneeded == false)
             _grounded = true;      //grounded = true
     }

 }

 void Flip(bool faceright)
     {
     if (faceright == true)
     {
         _playersprite.flipX = false;
             }

     else if(faceright == false)
     {
         _playersprite.flipX = true;
     }
 }
 IEnumerator resetjumpneededroutine()
 {
     yield return new WaitForSeconds(0.1f);
     resetjumpneeded = false;




 }

}

,using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine;

public class Player : MonoBehaviour { private Rigidbody2D _rigid; [SerializeField] private float _jumpforce = 5.0f; // variable for jumpforce [SerializeField] private bool _grounded = false; [SerializeField] private LayerMask _groundLayer; private bool resetjumpneeded = false; [SerializeField] private float _speed =5.0f; private Playeranimation _playeranim; private SpriteRenderer _playersprite;

        void Start()
 {
     _rigid = GetComponent<Rigidbody2D>();     // handle assign of rigidbody   
     _playeranim = GetComponent<Playeranimation>();  //handle assigned to _anim
     _playersprite   =  GetComponentInChildren<SpriteRenderer>(); 
 }

 // Update is called once per frame
 void Update()
 {
     Movement();
     checkgrounded();
       
 }

 void Movement()
 {

     float move = Input.GetAxisRaw("Horizontal");   //horizontal input for left /right movement

     if (move > 0 )
     {
         Flip(true);

     }

     else if (move < 0 )
     {
         Flip(false);
     }


     if (Input.GetKeyDown(KeyCode.Space) && _grounded == true)    //if space key && grounded == true
     {
         //jump 
         _rigid.velocity = new Vector2(_rigid.velocity.x, _jumpforce);
         _grounded = false;
         resetjumpneeded = true;
         StartCoroutine(resetjumpneededroutine());
         


     }
     
     _rigid.velocity = new Vector2(move* _speed, _rigid.velocity.y);  //current velocity = new velocity (current x ,jumpforce) 
     _playeranim.Move(move);
 }

 void checkgrounded()
 {

     RaycastHit2D hitinfo = Physics2D.Raycast(transform.position, Vector2.down, 0.6f, 1 << 8);   //2dray cast to the ground 
     Debug.DrawRay(transform.position, Vector2.down * 0.6f, Color.green); //this is to show the raycast in game


     if (hitinfo.collider != null)
     {
         Debug.Log("Hit: " + hitinfo.collider.name);

         if (resetjumpneeded == false)
             _grounded = true;      //grounded = true
     }

 }

 void Flip(bool faceright)
     {
     if (faceright == true)
     {
         _playersprite.flipX = false;
             }

     else if(faceright == false)
     {
         _playersprite.flipX = true;
     }
 }
 IEnumerator resetjumpneededroutine()
 {
     yield return new WaitForSeconds(0.1f);
     resetjumpneeded = false;




 }

}

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

125 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

Related Questions

(2D sidescrolling platformer) Flipping my character right or left depending on the mouse? 2 Answers

Having trouble with flipping my projectile. 0 Answers

Weird flipping gameobject 1 Answer

Sprite disapears when moving left 2 Answers

Problem with counting front flips 1 Answer


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