from world to local coordinates bouncing ball?

Hi,

I would like my character to bounce constantly, even if its parent moves up or down. If I try the bouncing effect on a constant y axis in local space, it works fine. But if the character goes down, the ball stays up in the air, because its “anchor point” has not changed. So I would like to use the character’s y position for my effect. How could I then update the local coordinates with the parent’s y position (which is in world space)? TransformPoint is to go from local to world, how to do the inverse? Or any other solution?

Here is the code that works in local space :

coef += 0.1f;
//Vector3 trPos = transform.position;
//trPos.y = posY + (Mathf.Sin(coef) * multSin);//local space : goes up and down

Vector3 trPos = transform.TransformPoint(transform.parent.position);//cannot use this to update local coordinates
trPos.y = trPos.y + (Mathf.Sin(coef) * multSin);
//transform.position = … new Vector3(trPos.x, trPos.y, trPos.z );

Thanks for your help

If I understand what you want to do correctly, what you want to do is to Transform.localPosition:

public class Bounce : MonoBehaviour {

	public float speed = 1.0f;
	public float magnitude = 2.0f;
 
	void Update() {
	    Vector3 v3 = transform.localPosition;
		v3.y = Mathf.Sin(Time.time * speed) * magnitude;
		transform.localPosition = v3;
	}
}

If you want more of a bounce like a ball hitting a surface, change the calculation to:

v3.y = Mathf.Abs(Mathf.Sin(Time.time * speed) * magnitude);