- Home /
Gameobject is climbing out objects with rigidbody
So I have a player, that is a tank. I have a bounding box on the tank and a rigidbody attached to it for collisions. I am using a joystick from the package EasyTouch 3 to use move the tank around. However, I am having difficulty with the movement of the tank and its interactions with the world. I understand WHY im having some of the issues I am having, just not sure on how to fix them, or the best approach.
So here is my setup: The movement joystick right now is simply sending its X and Y (from -1 to 1) to the camera script which is also moving the tank (and the camera is following the tank). The camera is looking down at the tank at about a 45 degree angle. When it receives the "on_joystickmove" event from the easytouch asset, it then moves the tank accordingly.
The asset does have a way of moving object on its own, however it's setup to have the joystick pointing up be forward, regardless of how the tank is facing. The mechanic I am going for is the tank goes in the direction the joystick is facing.. Point it left, it goes to the left, etc. So I am using the events that fire off to move my guy.
I had difficulty moving the tank with velocity, addforce, etc. So I used this:
Vector3 point = new Vector3(follow.transform.position.x + (move.joystickAxis.y * -2), follow.transform.position.y , follow.transform.position.z + (move.joystickAxis.x * 2));
follow.transform.position = Vector3.Lerp(follow.transform.position, point, Time.deltaTime * 8 * currentSpeed);
Vector3 turn = new Vector3(follow.transform.position.x + (move.joystickAxis.y * -2), follow.transform.position.y , follow.transform.position.z + (move.joystickAxis.x * 2));
follow.transform.rotation = Quaternion.Lerp(follow.transform.rotation, Quaternion.LookRotation(turn - follow.transform.position), Time.deltaTime * 5);
This is a basic lerp for movement. Basically I was projecting the x and y axis down to the x and z axis and picking a point right in front of the tank (or to the sides) and moving it to that position over time. I messed with the numbers and such to get it to a point where I like it. However this causes issues with collision because it will try and lerp it through a bounding box and cause the tank to climb any building, or have the rigidbody on it react to the phyics of hitting a box and overlapping it and cause the tank to fly in the air. Otherwise, the tank moves PERFECTLY in how I want it to move and interact with the joysticks.
So I know with the rigidbody I could add a force and it would stop on collision and not move, but I cannot seem to get this to work right. I have found a bunch of examples, and the car tutorial but none of these translate well into using a joystick (as they all use keys, or the input axis)
I have tired: follow.rigidbody.velocity = follow.transform.forward 15 currentSpeed;
Which moves it forward just fine, though im not getting the ease in and out effect of using the joystick not 1 values, so by multiplying those values in (as ABS) it kind of jumps around.
I also tried from the wiki: float x = move.joystickAxis.x 8; float y = move.joystickAxis.y 8;
if(follow.rigidbody.velocity.magnitude < 15)
{
follow.rigidbody.AddForce(follow.transform.rotation * Vector3.forward * y);
follow.rigidbody.AddForce(follow.transform.rotation * Vector3.right * x);
}
But it doesn't move at all. Any ideas would be greatly appreciated.
Your answer
Follow this Question
Related Questions
Kinematics: Trying to get an object to land at a known point 1 Answer
[Solved]Why doesn't this Rigidbody.AddForce work the way I tell it to? 1 Answer
Rigidbody movement 1 Answer
Directional rolling a ball ( upwards / left only ) 0 Answers
Rigidbody--Addforce on a Spherical Platform(A Globe) 2 Answers