I’m trying to move my object from point A to B without it snapping.
I have used:
Vector3.MoveToward
and
Vector3.Lerp
Maybe I am not using it right?
I also tried a While loop
while (HookShot == true)
{
if (HookShot == false)
{
break;
}
HookChild.Translate(Vector3.MoveTowards(Player.transform.position,HookHit.point,Mathf.Infinity));
}
Which basically caused a crash.
void Update ()
{
if (GameObject.FindGameObjectsWithTag("Hook").Length != 1)
{
HookChild = Instantiate(Hook,new Vector3(Player.position.x,Player.position.y,Player.position.z + 2)
,Quaternion.identity) as Transform;
HookChild.transform.parent = Player;
HookShot = false;
}
if(GameObject.FindGameObjectsWithTag("Rope").Length != 1)
{
RopeChild = Instantiate(Rope,new Vector3(HookChild.position.x, HookChild.position.y,HookChild.position.z)
,Quaternion.identity) as Transform;
RopeChild.transform.parent = Player;
RopeChild.localScale = new Vector3 (0.5f,2.0f,0.5f);
}
//-----------------------------------------------------------------------------------//
Ray HookRay = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
Debug.DrawRay(HookRay.origin,HookRay.direction,Color.green);
Vector3 curHookPos = HookChild.position;
Vector3 newHookPos = HookChild.position;
if (Input.GetMouseButtonDown(0))
{
if(Physics.Raycast(HookRay, out HookHit, 50.0f) == true)
{
HookShot = true;
Debug.DrawRay(HookRay.origin,HookRay.direction, Color.red);
Debug.Log("Hook was hit");
newHookPos = Vector3.Lerp(curHookPos,HookHit.point,speed * Time.deltaTime);
RopeChild.localRotation = Quaternion.LookRotation(Player.transform.forward,HookHit.point);
RopeChild.localScale += ropeMoveTowards * Time.deltaTime;
}
}
else if (Input.GetMouseButtonUp(0))
{
newHookPos = curHookPos;
RopeChild.rotation = Quaternion.identity;
RopeChild.localScale = new Vector3 (0.5f,2.0f,0.5f);
HookShot = false;
}
}
Ignore the Rope stuff, I’m just messing around with that.