- Home /
Continuing the momentum of the rigidbody
So, I have a basic cube with a rigidbody on it. Then I created a grapple hook script that uses a line to represent that line between the point it hits and the player. I got it to the point where the player does go to the spot of the point that was hit, but if I stop the grapple mid air, it stops all moment of the cube and it just drops straight down to the platform below it. I want to be able to continue that momentum of the cube so it moves forward as it still drops.
Script:
private void Movement()
{
float distance = Vector2.Distance(lineRen.GetPosition(1), hit.point);
if (hitObject)
{
if (distance < 1)
{
Vector3 movePath = (transform.position - hit.point).normalized;
RB.MovePosition(RB.position + movePath * Time.deltaTime * -pullStrength);
}
}
else
{
RB.velocity = RB.velocity;
}
}
private void SingleGrapple()
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (clickReady)
{
if ((Input.GetMouseButtonDown(0)))
{
if (Physics.Raycast(ray, out hit))
{
lineRen.SetPosition(1, transform.position);
hitObject = true;
clickReady = false;
}
}
}
if (hitObject)
{
lineRen.SetPosition(0, transform.position);
Vector3 newPos = Vector3.MoveTowards(lineRen.GetPosition(1), hit.point, Time.deltaTime * travelSpeed);
lineRen.SetPosition(1, newPos);
RB.useGravity = false;
}
}
There doesn't seem to be anything here indicating what changes when your grapple is "cancelled". Also, this block is pretty useless:
else
{
RB.velocity = RB.velocity;
}
Where are you cancelling the grapple and how does that influence these values?
The RB.velocity thing was something I forgot to get rid of. As for when it cancels, I have in the $$anonymous$$ovement() method is that it detects the distance between the the point the ray hits on an object and the position of the line end. I have set a bool to disable the ray part so the line goes to one point, and that is part of SingleGrapple() method. once the line end meets the hit point of the ray, it then does the $$anonymous$$ovement() method to pull the cube towards the point the line is. I have a if statement checking the input of $$anonymous$$ouse(0) to see if it is up, which then re-activates the ray firing trigger and disables the movement of the cube from going to the point.
Your answer
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Is it possible to increase stopping distance with forcemode.velocitychange? 0 Answers
Can i make more gripped rigidbody sphere Controllers 0 Answers
instantiated objects have different speed 1 Answer
Get velocity of point offset from center of rigidbody 1 Answer