- Home /
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612231117im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Picking up rigidbody objects 2 Answers
pick up item script issue 3 Answers
Problems with raycast 1 Answer