I’m trying to set an object to change it’s gravityScale by clicking a button, but it doesn’t seem to respond and I’m not sure why.
using UnityEngine;
using System;
using System.Collections;
public class Oxygen_Script : MonoBehaviour {
public GameObject[] goList;
public string atmosphere;
void Start () {
goList = (GameObject[])FindObjectsOfType (typeof(GameObject));
}
// Update is called once per frame
void Update () {
}
void OnMouseDown()
{
int max = goList.Length;
for (int i = 0; i < max; i++)
{
Debug.Log(goList[i].gameObject.name);
if (goList[i].gameObject.name == "Bubble")
{
//Rigidbody2D rb = goList[i].gameObject.GetComponent<Rigidbody2D> ();
//rb.gravityScale = 0;
goList[i].rigidbody2D.gravityScale = 0;
}
}
}
}
I’ve also tried setting the gravityScale to 0.0f it doesn’t work either. the current setting for the bubble object is -0.25
Are you actually looking at the public gravity scale for the object? Im currently struggling with making an object hover. It works to an extent. I can see the gravity scale set to zero, but there is still some sort of downward (maybe in your case upward since your gravity is negative to begin with) force im fighting against. Its causing rapid downward then upward movement thrashing.
public class PlayerBehavior : MonoBehaviour {
Vector3 jumping;
public int jumpingPower;
float currentY;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
ControllerInput ();
OP: are you sure your script is finding your “Bubbles” game object? Put a Debug.Log (or something) in that section to confirm it’s ever called in the first place.
Other commenter: try not to hijack threads with your own unrelated issues. To solve your problem, know that all physics forces should only be applied in FixedUpdate(), not Update(). Also, it looks like you are attempting to do several competing things (set a velocity directly, then set a transform position directly). These will absolutely interfere with each other. Also keep in mind gravity isn’t a translation of 9.8m per second but an acceleration by that amount.
Ok so I’ve decided to rework my script to see if I can get gravityScale to set at all, what I have right now is a script directly applied to the bubble object that in theory should change the gravityScale, but it doesn’t seem to want to change anything.
using UnityEngine;
using System.Collections;
public class Bubble : MonoBehaviour {
public string atmosphere;
public float oxygenVal = 0.0f;
public float heliumVal = 0.25f;
public float hexaneVal = -0.25f;
// Use this for initialization
void Start () {
this.rigidbody2D.gravityScale = oxygenVal;
atmosphere = "Helium";
}
// Update is called once per frame
void Update () {
if (atmosphere == "Helium")
{
this.rigidbody2D.gravityScale = heliumVal;
}
if (atmosphere == "Hexane")
{
this.rigidbody2D.gravityScale = hexaneVal;
}
else
{
this.rigidbody2D.gravityScale = oxygenVal;
}
}
}
To hover something I would simply to something like turning off gravity and/or freezing Y movement, either of which can be done with scripting as we know. Otherwise something has to keep tweaking the upward ConstantForce until it can be determined that Y movement is minimized, if you really desire the physics being true to reality.