Not able to add forces to a Rigid Body Game Object from a script attached to another Game object which is a Trigger.
Hello,
I am trying to simulate a bottling assembly line in unity, I got stuck with a problem.
I have bottle model as a Prefab and will Instantiate it in conveyor continuously in an interval and there is a trigger game object in the conveyor, it will make the Rigidbody move in the required direction once the bottle hits and stays in the trigger area.
Code to move on the Conveyor
public Rigidbody rb;
public float feedRate = 1.0f;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void OnTriggerStay()
{
//rb.useGravity = false;
rb.MovePosition(transform.position + transform.right * feedRate * Time.deltaTime );
}
I have attached this script to the bottle Prefab and it works without any problem, and at the end of the conveyor I have another trigger area which will pull the bottels up (like kind of pickup)
Script for Pulling the bottels
public Rigidbody rb;
public float speed;
// Use this for initialization
private void OnTriggerStay(Collider other)
{
rb.AddRelativeForce(transform.position + transform.up * speed, ForceMode.Force);
}
I am trying to add this scripts to the trigger Game Objects and access the bottel(Rigidbody) from there, But it is not working
Script I am trying to make it work
public float _speed = 1.0f;
public GameObject know;
public Rigidbody rbody;
private void Start()
{
rbody = know.GetComponent<Rigidbody>();
}
void OnTriggerStay(Collider coll)
{
if(coll.gameObject.tag == "Bottlewithcap")
{
Debug.Log("Hi");
rbody.MovePosition(transform.position + transform.right * _speed * Time.deltaTime);
}
}
It is taking the Rigid body of the game object in the Inspector in the trigger Game Object script, but the Addforce is not affecting the game object, The probelm is I can't add both the scripts to the bottle Prefab as it contridicts with each other. It will be a great help, if some one can explain what is wrong
I hope the explanation makes sense. please look at the attached pic for reference
Thanks in Advance.