Changing gravity scale through collisions
Hey there, I'm currently making a mario esk platformer and i'm trying to change the gravity scale of an object when it collides. My goal is for the object to fall right after my player jumps on it, but it doesn't change the gravity scale when I enter the trigger. Here's the code I'm working with. using System.Collections; using System.Collections.Generic; using UnityEngine; public class FallScript : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { void OnTriggerEnter2D(BoxCollider2D coll) { if (coll.gameObject.tag == "JumpObj") { //You would change "Enemy" to the exact name of whatever you called your tag - case sensitive coll.gameObject.GetComponent<Rigidbody2D>().gravityScale = 1.0f; //disable gravity on the collided object this.GetComponent<Rigidbody2D>().gravityScale = 0f; //disable gravity on the objected collided into (IE, this one) } } } }
Thanks!
Your answer
Follow this Question
Related Questions
Having my GameObject passing trough floor during one frame ( 2D Platformer ) 1 Answer
Broken Jump Physics 0 Answers
Collider gets sometimes stuck at wall 0 Answers
How to move rigidbodies with 2D Character controller (No rigidbody) 1 Answer
How to add force based on surrounding colliders in 2D? 0 Answers