RotateAround an object with clamped limits
Hi,
I have a sphere (player) and a capsule (stick). This player should be able to swing the stick (like a sword). Currently I am working with only one dimension and later, will incorporate two.
In the lower half of the screen, attached to the camera is an Image fully transparent with touchscreen controls. These return (currently) X location on the screen, based on the screen centre, and normalised. So I have a touch method called GetPositionNormalised() which will return -0.5 to +0.5 depending on where your touch finger is on the L-R axis of the screen. This all works
So, the capsule is a child of sphere, and there is a script associated with this. In the Script I have this:
void Start () {
rb = GetComponent<Rigidbody> ();
player = GameObject.FindGameObjectWithTag ("Player");
}
And in the Update I am getting totally confused with Rotate() RotateAround() Quaternions, Euler, etc.
What I want to do is set the Rotation of this capsule around the player, depending on this finger position (-0.5 to +0.5), where I also set a float MaxRotation = 90, which means I want to limit this rotation to 90degrees left or right.
If I use Rotate() then the object will rotate around it's centrepoint. If I use RotateAround(player...) it works, but I can't get the limits working in code.
Currently I use the POSITION of the touch to SET the rotation of the capsule. This means a tap to the left will immediately set the position left, and a tap on the right will immediately jump to the right. This works, but doesn't feel right. However the clamping here works. I just want something more natural,
void Update () {
Vector2 touchPosNormalised = touchPadHandler.GetPositionNormalised ();
float maxRotation = 90.0f;
float targetRotationalValue = (2f * maxRotation * touchPosNormalised.x)
transform.rotation = Quaternion.Euler(0f,0f,-targetRotationalValue)
}
As you can see here, it rotates around its own centre.
What I want to do is rotate the capsule anchored by its base (ie connected to the player sphere), and also somehow Lerp or SLerp the movement for some smoother moving, and not jumping exactly to the position.
Many thanks