Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fikzy · Nov 09, 2014 at 02:17 PM · positiondetectionplacement

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 !

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Linus · Nov 09, 2014 at 02:24 PM 0
Share

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?

avatar image Fikzy · Nov 09, 2014 at 05:23 PM 0
Share

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 ^^

avatar image Linus · Nov 09, 2014 at 09:09 PM 0
Share

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.

avatar image Fikzy · Nov 09, 2014 at 09:46 PM 0
Share

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..

avatar image Linus · Nov 09, 2014 at 10:03 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges