- Home /
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.
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;
}
}
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);
}
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.
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;
}
}
Thank you very, very much. After all it's working now. Now I can continue to work on my project. Thank you!!!