- Home /
Unstable script
So i made a script that check if an object is adjacent to any object and connect it to the cockpit (robot's main part) if so. But the problem is that it's very unstable : sometimes it works, sometimes it doesn't work..
I can't really figure out why..
Here's the script :
public class ObjectPlacement : MonoBehaviour {
PlayerInteractions playerInteractionsScript;
public bool connected = false;
public bool connectedToCockPit = false;
public bool grabbed = false;
public int connectPointsCount;
public Vector3[] connectPointsPos;
public GameObject[] triggersDetectionGO;
public bool[] connectPointsConnected;
public bool[] connectPointsConnectedToCockpit;
public LayerMask layerMask;
public GameObject[] allPartObjects;
void Start () {
playerInteractionsScript = Camera.main.GetComponent<PlayerInteractions> ();
connectPointsConnected = new bool[connectPointsCount];
connectPointsConnectedToCockpit = new bool[connectPointsCount];
allPartObjects = GameObject.FindGameObjectsWithTag ("RobotConstructionPart");
}
void Update () {
for (int connectPoint = 0; connectPoint < connectPointsPos.Length; connectPoint++) {
Debug.DrawRay(transform.position + connectPointsPos[connectPoint], connectPointsPos[connectPoint], Color.red, 1);
RaycastHit hit;
foreach (GameObject objectPart in allPartObjects) {
if (objectPart.transform.position == transform.position + (connectPointsPos[connectPoint] * 2)) {
connectPointsConnected[connectPoint] = true;
connected = true;
if (objectPart.transform.name == "Cockpit" || objectPart.transform.GetComponent<ObjectPlacement>().connectedToCockPit) {
connectPointsConnectedToCockpit[connectPoint] = true;
connectedToCockPit = true;
}
}
else {
connectPointsConnected[connectPoint] = false;
connectPointsConnectedToCockpit[connectPoint] = false;
}
}
}
if (connectPointsConnected.All(connectPointConnected => !connectPointConnected)) {
connected = false;
}
if (connectPointsConnectedToCockpit.All(connectPointConnectedToCockpit => !connectPointConnectedToCockpit)) {
connectedToCockPit = false;
}
if (transform.name != "Cockpit") {
if (connected || connectedToCockPit) {
transform.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
} else {
transform.rigidbody.constraints = RigidbodyConstraints.None;
}
} else if (transform.name == "Cockpit" && !playerInteractionsScript.hover) {
if (connected || connectedToCockPit) {
transform.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
} else {
transform.rigidbody.constraints = RigidbodyConstraints.None;
}
}
if (grabbed) {
for (int connectPoint = 0; connectPoint < triggersDetectionGO.Length; connectPoint++) {
triggersDetectionGO[connectPoint].SetActive(false);
}
rigidbody.useGravity = false;
} else if (!grabbed) {
for (int connectPoint = 0; connectPoint < triggersDetectionGO.Length; connectPoint++) {
triggersDetectionGO[connectPoint].SetActive(true);
}
rigidbody.useGravity = true;
}
}
}
I've got an other script that place objects exacly 1 unit apart from the block i'm looking at, here is the line of code that place the object :
grabbed.transform.position = hit.collider.transform.position + (hit.collider.transform.forward / 2) ;
If you need any other precisions tell me. Thanks in advance !
Woha that is alot of code for us to guess whats wrong on :) Could you tell whats the symptom of it not working? Define unstable?
Hum, how to explain..
Sometimes the objects are not detected, i think it's due to line 34 :
if (objectPart.transform.position == transform.position + (connectPointsPos[connectPoint] * 2)) {
$$anonymous$$y objects have rigidbody's so the position is not a 100% precise..
If you want an idea of what i'm doing, look at this game, TerraTech : http://terratechgame.com/ I might be going completely in the wrong direction to acheive this..
If really needed, i could give you my project ^^
Yes explaining problems can be a challenge in it self. $$anonymous$$ain reason why I rarely ask for support. On the other hand some times I have solved the problem when typing the explanation.
I will try to see whats going on in the script and perhaps create a test scene. What I do int these situations is place Debug.Log all around the script and check the values of the variables and look for patternes in when things go wrong.
So little modifications have been made.
I've replaced these lines of code :
foreach (GameObject objectPart in allPartObjects) {
if (objectPart.transform.position == transform.position + (connectPointsPos[connectPoint] * 2)) {
connectPointsConnected[connectPoint] = true;
connected = true;
if (objectPart.transform.name == "Cockpit" || objectPart.transform.GetComponent<ObjectPlacement>().connectedToCock$$anonymous$$) {
connectPointsConnectedToCockpit[connectPoint] = true;
connectedToCock$$anonymous$$ = true;
}
}
else {
connectPointsConnected[connectPoint] = false;
connectPointsConnectedToCockpit[connectPoint] = false;
}
}
With :
if (Physics.Raycast(transform.position, connectPointsPos[connectPoint], out hit, 0.51f, layer$$anonymous$$ask)) {
if (hit.transform.tag == "RobotConstructionPart" && hit.transform.position == transform.position + (connectPointsPos[connectPoint] * 2)) {
hit.transform.position = transform.position + (connectPointsPos[connectPoint] * 2);
connectPointsConnected[connectPoint] = true;
connected = true;
if (hit.transform.name == "Cockpit" || hit.transform.GetComponent<ObjectPlacement>().connectedToCock$$anonymous$$) {
connectPointsConnectedToCockpit[connectPoint] = true;
connectedToCock$$anonymous$$ = true;
}
}
} else {
connectPointsConnected[connectPoint] = false;
connectPointsConnectedToCockpit[connectPoint] = false;
}
And it seems to work better, but is still very unstable..
I think it's due to physics, like 0.99962 ins$$anonymous$$d of 1.0 for the z position..
I think I am starting to understand the purpose of the script. Perhaps you should check distance ins$$anonymous$$d of if they are equal.
http://docs.unity3d.com/ScriptReference/Vector3.Distance.html
Answer by unimechanic · Nov 11, 2014 at 04:53 PM
You are doing physics operations inside Update, instead of FixedUpdate, that could make the scene unstable. Check out this answer:
http://answers.unity3d.com/questions/804064/interpolation-with-jumping-acting-weird.html
Your answer
Follow this Question
Related Questions
Am I inside a volume? (without colliders) 3 Answers
position coins in parabolic path 1 Answer
Camera rotation around player while following. 6 Answers
Detect exactly where other objects around player are 3 Answers
How do I randomly place doors, treasure chests, and traps in a randomly generated maze? 0 Answers