- Home /
Problem stopping rigid body after adding a force
In my game I have a balance board and a character that hops around on the board from being pushed with a relative force on its x-axis. I am trying to mimic Crossy Roads character movement, so I added a jump animation that parents my character object to change the y-axis by adding one to mimic a jump which works perfectly fine. The problem is I cannot use lerp the position because it doesn't work well moving my character (rigid body) on a balance board (rigid body). Therefore I have added a "if" statement that check when my character has reached its current x-position + 1 (local scale), when true I set my characters rigidbody velocity to zero to make it seem like you jump to a position on the board. When the board moves to fast my character seems to be floating, or when my character jumps off the board it floats in the air and slowly falls. I know the character floats in the air because I set the velocity to zero in my update and it only moves to a new position when I tap again. I have tried setting a timer on how long the velocity is set to 0, and I tried setting the x-velocity to zero and the y-velocity to -9.8 (gravity) but this seems to randomly jump in the air really high. SORRY for the rambling but all I need to set is the x- velocity to zero and keep the current state of the y-velocity to stop the movement on its local a-axis after a force has been applied and its "Jump To" position has been reached. Thank you m8s.
sing System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Jumpy : MonoBehaviour
{
float lastPosition;
bool completion = true;
Rigidbody rb;
public Animator ani;
public void Start() {
rb = GetComponent<Rigidbody> ();
}
public void Update() {
lastPosition = transform.position.x;
//when moving right
//when hit position to stop velocity act as a jump to
if (lastPosition >= (clickPosition + 0.85f)) {
rb.velocity = new Vector3 (0, 0, 0);
completion = true;
ani.SetBool ("Jump", false);
}
}
if(completion == true) {
//when up adds a force to push rb to position
if (Input.GetMouseButtonUp (0)) {
completion = false;
clickPosition = transform.position.x ;
}
}
//just little animation things works fine if any one wants to copy feel free to make a cool jumping squish
animation
if (Input.GetMouseButton (0)) {
//ani.SetBool ("HoldDown", true);
if (left == true) {
if (transform.localScale.y >= 0.8f) {
transform.localScale -= transform.up * Time.deltaTime * 1.2f;
transform.localScale -= transform.forward * Time.deltaTime;
}
}
} else {
//ani.SetBool ("HoldDown", false);
transform.localScale = Vector3.Lerp(transform.localScale,new Vector3(1.15f, 1.15f, 1.15f), Time.deltaTime * 10);
}
if (completion == false) {
ani.SetBool("Jump",true);
rb.AddRelativeForce (new Vector3 (1.5f, 0, 0), ForceMode.VelocityChange);
}
}
}
I don't know if I'm understanding your conditions correctly, but can't you just set only the X velocity, like rb.velocity *= new Vector3 (0, 1, 1);
, or rb.velocity = new Vector3 (0, rb.velocity.y, rb.velocity.z);
?
Works great! thanks a-lot cheers!