Resize a one child object with raycasting or collision
Hi,
I'm trying to figure out the best way to resize the length of the shaft of a golf club while keeping the scale of the handle and head intact. Essentially it's a golf club that adjust its length when it's about to hit the ground.
My club has a handle, a shaft and a head.
At the moment I'm trying raycasting from the head when it's about to hit the ground but I have issues when that if I just adjust the shaft length the handle and head are still in the same place and if I adjust the length of the whole club the club head shrinks as well.
The follow up problem is to make the shaft longer again up till its original length when it's being pulled away from the ground.
Anyone who have some suggestions on how to approach these problems? Is colliders maybe a better approach?
Here's what I've started with =)
public class AdjustShaft : MonoBehaviour {
public float detectionHeight;
public GameObject putter;
private Transform shaftTransform;
void Start(){
shaftTransform = putter.gameObject.transform.GetChild (2);
}
void FixedUpdate () {
RaycastHit hit;
Ray shrinkRay = new Ray (transform.position, Vector3.down);
Debug.DrawRay (transform.position, Vector3.down * detectionHeight);
if (Physics.Raycast (shrinkRay, out hit, detectionHeight)) {
if (hit.collider.tag == "Green") {
shaftTransform.localScale-= new Vector3(0, 0.1f, 0);
}
}
}
}
Thanks!