- Home /
Question by
T0talfail · Sep 26, 2017 at 01:44 PM ·
physics settings
Grappling hook max speed
So, I managed to make a grappling hook using this script, but it moves way to fast,
public RaycastHit hit;
public LayerMask cullingmask;
public bool isFlying;
public Vector3 loc;
public float speed = 10;
public int maxDistance;
void Start() {
}
public void Update()
{
if (Input.GetKeyUp(KeyCode.R))
{
HookAttach();
}
if(isFlying == true)
{
Flying();
}
}
public void HookAttach()
{
if (Physics.Raycast(transform.position, transform.forward, out hit, maxDistance))
{
isFlying = true;
loc = hit.point;
}
}
public void Flying()
{
transform.position = Vector3.MoveTowards(transform.position, loc, maxDistance);
if(Vector3.Distance(transform.position, loc) < 0.5)
{
isFlying = false;
}
}
any ideas on how to limit the speed?
Comment
Answer by wornTunic · Sep 26, 2017 at 02:11 PM
In the 40th line of code, try changing maxDistance to something like this:
float step = Time.deltaTime * speed;
transform.position = Vector3.MoveTowards(transform.position, loc, step);
and tweak the speed parameter to make it how you want it to be. Took this from https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html , so have a look at it.