- Home /
Problems with velocity
Im trying to get an object to return to the player by adding velocity to it to return it. the problem is that when I apply the velocity to the object, it applies that force to the player as well as the object meaning that they meet in the middle instead of the object coming back to the player on its own.
public class pl_GrapplingHook : MonoBehaviour
{
private Rigidbody rig;
public GameObject Hook;
public GameObject grappleObject;
public GameObject projSpawner;
private bool projShot;
public bool rangeOut;
public bool usingGravity;
private void Start()
{
rig = GetComponent<Rigidbody>();
projShot = projSpawner.GetComponentInChildren<ProjectileSpawner>().hasShot;
rangeOut = false;
usingGravity = rig.useGravity;
}
void Update()
{
if (Hook != null)
{
// direction player is launched twards
Vector3 hookPos = (Hook.transform.position - transform.position).normalized;
// direction hook is launched twards
Vector3 hookreturn = (transform.position - Hook.transform.position).normalized;
//modifying the hook angle for return
Rigidbody rig_Hook = Hook.GetComponent<Rigidbody>();
Hook.transform.forward = hookreturn;
rig.velocity = hookPos;
if (usingGravity == true)
{
print("fire1");
rig.AddForce(rig.velocity * 2, ForceMode.Impulse);
}
else if (usingGravity == false)
{
print("fire2");
rig.AddForce(rig.velocity * 30, ForceMode.Impulse);
}
if (Input.GetMouseButtonDown(1))
{
rig_Hook.isKinematic = false;
rig_Hook.useGravity = false;
// sending the hook back to the player
rig_Hook.velocity = hookreturn * 10;
Hook.GetComponent<SphereCollider>().isTrigger = true;
Hook.GetComponent<SphereCollider>().enabled = true;
}
}
}
}
initially the object is launched from the player as a grappling hook and then the player is launched forward when it attaches to something, however the problem occurs when the button is pressed and the object starts returning to the player. any idea what is happening there?
Your answer
Follow this Question
Related Questions
Inconsistent jump height 1 Answer
[C#, 2D] How do I apply force to a player using vector 3 velocity to move 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Inconsistent jump height 1 Answer