Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Porcus · Aug 16, 2017 at 12:26 PM · unity 5gravityspriterendererflip

Gravity Change + Flip

Hello Everybody,

I try to make a scene (only one scene, not a whole game) like VVVVVV. So far my code is working, but I don't know how to "flip back". When I press Space, the gravity changes and my Player-Gameobjects flips. But when I press the key again, it doesn't flip back (flipY => false), although the gravity changes again.

If anyone knows how to make my code fully working, please help me. I try to fix it since five days.

Thank you in advance.

Comment
Add comment · Show 1
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
avatar image Porcus · Aug 16, 2017 at 12:31 PM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BrainP$$anonymous$$ovement : $$anonymous$$onoBehaviour {
 
     public float speed = 5.0f;
     public bool isOnGround = false;
     public bool flipY;
 
 
     private Transform _transform;
     private Rigidbody2D _rigidbody;
     private SpriteRenderer mySpriteRenderer;
 
     // Use this for initialization
     void Start () {
         _transform = GetComponent (typeof(Transform)) as Transform;
         _rigidbody = GetComponent (typeof(Rigidbody2D)) as Rigidbody2D;
     }
 
 
     // Update is called once per frame
     void Update () {
         $$anonymous$$ovePlayer ();
         ChangeGravity ();
         mySpriteRenderer = GetComponent<SpriteRenderer>();
 
 
     }
 
     void $$anonymous$$ovePlayer() {
         float translate = Input.GetAxis ("Horizontal") * speed * Time.deltaTime;
         _transform.Translate (translate, 0, 0);
 
     }
 
     void ChangeGravity (){
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) && isOnGround && mySpriteRenderer.flipY != null) {
             Physics2D.gravity *= -1f;
             mySpriteRenderer.flipY = true;
         }
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.JoystickButton1) && isOnGround && mySpriteRenderer.flipY != null) {
             Physics2D.gravity *= -1f;
             mySpriteRenderer.flipY = true;
 
         }
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
         }
     }
 
     void OnCollisionEnter2D() {
         isOnGround = true;
     }
 
     void OnCollisionExit2D() {
         isOnGround = false;
     
 
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by M-G-Production · Aug 16, 2017 at 05:27 PM

I suppose you simply flip the SpriteRenderer and you work with gravityScale (from Rigidbody2D)? If so, let's make it super simple then.

 public SpriteRenderer mySprite;
 public Rigidbody2D myR;
 
 public void Update()
 {
     mySprite.flipY = (myR.gravityScale < 0);
 }
Comment
Add comment · Show 3 · Share
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
avatar image Porcus · Aug 16, 2017 at 06:30 PM 0
Share

Thank you very much for your help. I'm glad someone helped me. I forgot to add my code, which was added by me as a comment later. BUT I don't get your code work. I tried to implement it, but it didn't work properly again. Everytime I press SPACE, the player "floats" to the ceiling and flips, if I press SPACE again, he "floats" back but doesn't not flip.

If you've any idea what I should change in my code I would be very grateful.

Thanks in advance again.

avatar image M-G-Production Porcus · Aug 16, 2017 at 07:52 PM 1
Share

Hey bud! I've corrected your script. I saw a couple of $$anonymous$$or mistakes and it should now work perfectly!

By the way I added 1 variable, it allows you to choose if you only want the player to reverse gravity or the world!

If it works, please define my Reply as the Answer and rate it up :3 $$anonymous$$y pleasure:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BrainP$$anonymous$$ovement : $$anonymous$$onoBehaviour
 {
 
     public float speed = 5.0f;
     public bool isOnGround = false;
     public bool flipY;
     
     private Transform _transform;
     private Rigidbody2D _rigidbody;
     private SpriteRenderer mySpriteRenderer;
 
     //Stuff Added...
     public bool onlyAffectPlayer = true;
 
     // Use this for initialization
     void Start()
     {
         _transform = GetComponent(typeof(Transform)) as Transform;
         _rigidbody = GetComponent(typeof(Rigidbody2D)) as Rigidbody2D;
 
         //You put this line on Update method, it's better to put it in Start!
         mySpriteRenderer = GetComponent<SpriteRenderer>();
     }
 
     // Update is called once per frame
     void Update()
     {
         $$anonymous$$ovePlayer();
         ChangeGravity();
     }
 
     void $$anonymous$$ovePlayer()
     {
         float translate = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         _transform.Translate(translate, 0, 0);
     }
 
     void ChangeGravity()
     {
         //If you press (Space or JoystickButton) and you are grounded
         if ((Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.JoystickButton1)) && isOnGround)
         {
             //If you only want the player to be affected, use these next lines
             if (onlyAffectPlayer)
             {
                 //Revert it's own gravity
                 _rigidbody.gravityScale *= -1;
 
                 //Flip the sprite
                 mySpriteRenderer.flipY = (_rigidbody.gravityScale < 0);
             }
             else
             {
                 //Otherwise, you can change the Physics Gravity
                 Physics2D.gravity *= -1;
 
                 //And Flip the sprite
                 mySpriteRenderer.flipY = (Physics2D.gravity.y < 0);
             }
 
             //Store your new value in your variable
             flipY = mySpriteRenderer.flipY;
         }
     }
 
     void OnCollisionEnter2D()
     {
         isOnGround = true;
     }
 
     void OnCollisionExit2D()
     {
         isOnGround = false;
     }
 }
 
avatar image Porcus M-G-Production · Aug 17, 2017 at 06:53 AM 1
Share

Thank you very, very much. After all it's working now. Now I can continue to work on my project. Thank you!!!

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

140 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

Related Questions

Anti-gravity is turning on when it shouldn't 1 Answer

Why do my sprites keep disappearing after building the game and playing? 0 Answers

Flip gravity 0 Answers

How do I get the platform to effect these coins? 2 Answers

Unity 2D, Flip Sprite without using a rigidbody 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