- Home /
Question by
finlay_morrison · Sep 17, 2017 at 03:33 PM ·
c#unity 5scripting problemrigidbodygravity
rigidbody.Use gravity not working from script
so i have 10 pins and when i call the Raise() from the animator they are going up 40 world units like they are supposed to however they are still using gravity after i set rigidBody.UseGravity to false; please tell me why
Here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Pin : MonoBehaviour {
public float standingThreshold;
public float distanceToRaise = 40f;
private Rigidbody rigidBody;
void Start() {
rigidBody = GetComponent<Rigidbody>();
}
void Update() {
IsStanding();
}
public bool IsStanding() {
Vector3 rotationInEuler = transform.rotation.eulerAngles;
float tiltInX = Mathf.Abs(rotationInEuler.x - 270);
float tiltInZ = Mathf.Abs(rotationInEuler.z);
if (tiltInX < standingThreshold && tiltInZ < standingThreshold) {
return true;
}
else {
return false;
}
}
public void Raise() {
foreach (Pin pin in GameObject.FindObjectsOfType<Pin>()) {
if (pin.IsStanding()) {
print("Raising Pin");
rigidBody.useGravity = false;
pin.transform.Translate(new Vector3(0, distanceToRaise, 0), Space.World);
}
}
}
public void Lower() {
foreach (Pin pin in GameObject.FindObjectsOfType<Pin>()) {
pin.transform.Translate(new Vector3(0, - distanceToRaise, 0), Space.World);
rigidBody.useGravity = true;
}
}
}
Comment
Hmm, is it possible there pins are connected to another object with a joint of some kind, or via a collision. Perhaps the jointed/colliding objected DOES still have gravity enabled, and is pulling/pushing the pins with it? I would suggest creating an empty scene, with just a pin, and see if results change. If so, that would indicate this is due to a scene configuration.