• 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 hestermannelke · Apr 11, 2020 at 02:30 PM · raycastpickuprobot

The problem I am having is that my robot is picking up all the disk on my belt instead of the one that has hit my raycast.

// I am currently coding a pick up robot system. I have two color disks(blue and red) that randomly spawn on a conveyor belt. The robot must then pick up the disk once it is at the end of the belt and then place on the correct color belt. The problem I am having is that my robot is picking up all the disk on my belt instead of the one that has hit my raycast. How do I ensure that the robot only picks up the one disk instead of all my spawned disks? This is my sensor's code to detect which color disk and my pickup code where the robot must pick up the disk is below.

 public float beltWidth = 3;
 public GameObject sensor;
 public int stopFlag = 0;
 public int blueFlag = 0;
 public int redFlag = 0;
 private ADSConnection TwinCAT;

 void Awake()
 {
    // TwinCat is the program I am using to control the robot.
     TwinCAT = GameObject.FindObjectOfType<ADSConnection>();
 }
 
 void Update()
 {
     Ray stopRay = new Ray(transform.position, Vector3.forward);
     RaycastHit hit;

     Physics.Raycast(stopRay, out hit, beltWidth) ;

     //Creates a sensor line for visual effects
     Debug.DrawLine(transform.position, transform.position + Vector3.forward * beltWidth, Color.red);

     Debug.Log(hit.point);
   
     if(Physics.Raycast(stopRay, out hit, beltWidth) ){

         if (hit.collider.tag == "blue")         //Set Blue flag
         {
             TwinCAT.stopFlag = 1;             // Flag that stops conveyor belt speed
             TwinCAT.blueFlag = 1;

         }
         else if (hit.collider.tag == "red")       //Set Red flag
         {
             TwinCAT.stopFlag = 1;
             TwinCAT.redFlag = 1;
          }
   
 }  

// Pick Up code:

 private ADSConnection TwinCAT;
 public int suction;

 // Use this for initialization
 void Start () {
     TwinCAT = GameObject.FindObjectOfType<ADSConnection>();
   }
 
 void Update () {

     // Suction is a variable read from TwinCat. This was a requirement of the project.
     suction = TwinCAT.suction;

     if (suction == 1)
     { 
   //Picks up disk and TCP is the game object at the end of the robot where the disk and robot connect.
         GetComponent<Rigidbody>().useGravity = false;
         this.transform.position = GameObject.Find("TCP").transform.position;
         this.transform.parent = GameObject.Find("TCP").transform;

     }
     else {
   // No pick up required
         this.transform.parent = null;
         GetComponent<Rigidbody>().useGravity = true;

     }
   }
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Picking up rigidbody objects 2 Answers

pick up item script issue 3 Answers

Problems with raycast 1 Answer

Raycasting to pick up an item problem 1 Answer

[Problem] Cant activate 2D objects with touch? 1 Answer


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