I have a GameObject which is the parent to a number of other GameObjects (planes), which are jointed together to create head, torso, arms and legs.
These children have box colliders and rigidbodies, whereas the parent only has a rigidbody (which I think may not actually be required…).
I have a script which is supposed to move the entire object forward when any of the child objects are touched (using ScreenPointToRay). What I can’t figure out is how to make a touch on one part of the construction move all the other parts uniformly.
If I put this script, which tries to move the parent on the torso piece, then nothing happens.
void FixedUpdate () {
RaycastHit rayHit = new RaycastHit();
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayHit)) {
if (Input.GetMouseButtonUp(0)) {
if (rayHit.transform == this.transform) {
this.transform.parent.gameObject.rigidbody.MovePosition(this.transform.parent.gameObject.rigidbody.position + speed * Time.deltaTime);
//this.transform.parent.gameObject.transform.position = new Vector3(this.transform.parent.gameObject.transform.position.x, this.transform.parent.gameObject.transform.position.y, this.transform.parent.gameObject.transform.position.z - 3.71f);
}
}
}
}
But, if I put this second script on the torso, then the whole thing goes nuts, with the jointed parts trying to fly off and, well, it’s just chaos. Very amusing to watch, though.
void FixedUpdate () {
RaycastHit rayHit = new RaycastHit();
if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out rayHit)) {
if (Input.GetMouseButtonUp(0)) {
if (rayHit.transform == this.transform) {
this.rigidbody.MovePosition(this.rigidbody.position + speed * Time.deltaTime);
//this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z - moveAmount);
}
}
}
}
I’ve tired various variations on both themes, but I’m just not quite getting it.