Gravity problem on Rigibody2D (Unity)

Updated on January 2, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on January 2, 2018

I’m writing a script in C# in Unity that essentially functions as a switch to turn gravity on or off for a 2D Rigidbody. When the game starts, gravity should be 0 for the Rigidbody. Then when the user taps the space bar, gravity is supposed to increase to 3 (which is does). Then, however, when the player collides with a gameObject labeled ‘InvisGoal’, the player should teleport to another location and then gravity should be 0 again. However, the player always falls after coming in contact with InvisGoal and teleporting and I can’t figure out why. This is my first project in C# so sorry for any obvious errors.. The script is here:

  using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 public class DeathScreen : MonoBehaviour {
     public GameObject ballNum;
     void OnTriggerEnter2D(Collider2D other){
         if (other.tag == "InvisGoal") {
             ballNum.gameObject.GetComponent<Rigidbody2D>().gravityScale = 0f;
             transform.position = new Vector3 (0.61f, 1.18f, 0f);
         if (other.tag == "InvisKill") {
             Debug.Log ("this thing isnt working");
             Destroy (ballNum);
             UnityEngine.SceneManagement.SceneManager.LoadScene ("DeathScreen");
  • Liked by