To clarify when the player reaches the boundaries of the rope(tether) the player just stays in one position.
The rope uses raycast to attach the rope and move the player.
The Player uses the Sample Assets fps script(uses a rigidbody).
Code(attached to camera):
public class Swinging : MonoBehaviour
{
//labels tell the story of what its for
public bool amITethered = false;
public float maximumTetherLength = 15f;
public Transform avatar;
private Vector3 testPos;//keeps the code neat
private Vector3 tetherPoint;
private float tetherLength;//actually has no idea what this is for
private void Update ()
{
testPos = avatar.position - tetherPoint;
float testFloat = testPos.magnitude;
AttachTether();
if(amITethered)
{
Debug.DrawLine(avatar.position, testPos.normalized * maximumTetherLength, Color.green);//where the testTrans vector is located
if(testFloat > maximumTetherLength)//creates a spherical boundary
{
print("Hitting the boundary");
Vector3 testTrans = (avatar.position - tetherPoint).normalized * maximumTetherLength;//tetherLength; //adding tetherLength does something weird || making it negative makes it weirder
avatar.position = testTrans;//moves the character
}
}
}
private void AttachTether()
{
RaycastHit wallData;
if(Input.GetButtonDown(Tags.mouse1))
{
if(Physics.Raycast( transform.position, transform.forward, out wallData, maximumTetherLength ))//attaching tether
{
amITethered = true;
tetherPoint = wallData.point;//the imaginary attch point
tetherLength = (wallData.point - avatar.position).magnitude * maximumTetherLength;//dont know what this is for
Debug.DrawLine(avatar.position, tetherPoint, Color.cyan);//find out where I attach the tether
Debug.DrawLine(avatar.position, wallData.point - avatar.position * tetherLength, Color.magenta);//find out where the tether length is and how long is it
}
}
}
}
[Resource to the code][1]
[1]: http://gamedevelopment.tutsplus.com/tutorials/swinging-physics-for-player-movement-as-seen-in-spider-man-2-and-energy-hook--gamedev-8782
↧