- Home /
AddRelativeForce not working correctly
I am stuck on the code for couple of days but couldn't figure it out. Therefore I turned out to look the answer here but couldn't find any that match the requirement Hope some One might help
here is the code
/* Part1
*Step Horizontal Movement
* Use to get left and right movement
*/
if(Input.GetKey ("right")||Input.GetKey ("left")){
moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
}
rigidbody.velocity = moveDir * speed;
/*Part2
*Continuse Forward Movement
*/
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
moveDir.z = Input.GetAxis("Vertical");
rigidbody.AddRelativeForce(moveDir*50);
}
I have a Cube with added rigidBody Component. When pressed left or right key the cube should move in that direction and when key is released it should stop Part one when run commenting part2 code runs correctly
the other movement that I am trying to achieve is the cube should move continuously.But the thing is when pressed up key it should increase the magnitude and when released it should maintain the magnitude and move constantly.This is also achieved successfull with the part2 code by commenting part1 code But when but run without commenting any of them part1 results are correct but part2 does not provide correct result which was obtained by commenting part1 code
Is there any other way to obtain the same reult
Your problem is easily solved if the cube remain axes aligned. It is more work if the object rotates. From your code I cannot tell. Part 1 suggest it is aligned since you are directly setting the Rigidbody with a world direction. Part 2 suggests that it is not since you use AddRelativeForce() ins$$anonymous$$d of AddForce(). So does the object remain aligned? Can the object collide with other objects and if so, have you frozen the rotation in the Rigidbody constraints?
Yes the object remain aligned and it collide with the other object and rotational component of the object is frozen
Answer by robertbu · Jul 26, 2014 at 04:30 PM
You don't give me enough code to tell if you are using Javascript or C#. Here is a C# solution (untested):
Vector3 vel = rigidbody.velocity;
vel.x = Input.GetAxisRaw("Horizontal") * speed;
rigidbody.velocity = vel;
rigidbody.AddForce (Vector3.forward * Input.GetAxis("Vertical") * 50.0f);
First the Input.GetKey() tests are unnecessary. If you look at how the Horizontal and Vertical Axes are defined, you will see they have the keys you are using defined. Also there is a GetAxisRaw() that gets the the raw data. GetAxis() is buffered a bit. You may like the way your game feels with GetAxis() rather than GetAxisRaw(), but your question indicated an immediate stop.
Note the way this problem is solves is to have part 1 only manipulate the 'x' axis, and part 2 only manipulate the 'z' axis of the object.
If you are using Javascript/Unityscript, it can be simplified to:
rigidbody.velocity.x = Input.GetAxisRaw("Horizontal") * speed;
rigidbody.AddForce (Vector3.forward * Input.GetAxis("Vertical") * 50.0f);
Thanks a lot this is all what I wanted...:D But there is a new issue that had arrived this code is working in excellent way when gravity is turn off and turning off the Gravity the cube doesn't jump.And keeping the Gravity on forces the cube to halt after traveling certain distance
Start by setting a Physics material with little or no friction for colliders on both the object and the surface it rides on. If that is not enough, then set velocity ins$$anonymous$$d of doing AddForce(). You can do:
vel = rigidbody.velocity;
vel.z += Input.GetAxis("Vertical") * factor;
...where 'factor' is a value you tune to get things the way you want (assu$$anonymous$$g a linear progression works for you).
This is the script that I am Using
void Start(){
// get the distance to ground
distToGround = collider.bounds.extents.y;
}
bool IsGrounded() {
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1);
}
void Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && IsGrounded()){
rigidbody.velocity = new Vector3 (0,jumpSpeed, 0);
print("I am on");
}
}
This script is not working wothout gravity.
I'm not sure what you want to do. The code I posted in my last comment will overcome the slown-down issue, even with gravity turned on. You can use an alternate definition for grounded that will work without gravity, but then you'd also have to include alternate to gravity to move your object back down after a jump.
Your answer
Follow this Question
Related Questions
Velocity powered rigidbody on a moving platform without parenting. 3 Answers
Sum of multiple velocities 1 Answer
Rigidbody Character 1 Answer
rigidbody.velocity.normalized application 1 Answer
Why is velocity checking intensive? 0 Answers