1
votes

I am making a game in unity. I have a tank it's got a rigidbody. I am using transform.translate to move it. When it gets hit by a shot it moves backwards. If use the joystick to move it forward it moves with reduced speed. If I let go of the joystick it continues moving backward. Please help.

1
Welcome to StackOverflow! Please add you code as a Minimal, Complete, and Verifiable example - derHugo

1 Answers

1
votes

There is not much to go in your question but I'm quite certain you're moving the character with transform.translate (like you wrote) but you have a Rigidbody attached to the GameObject, which uses physics.

So when your GameObject gets hit by a projectile the physics will move it backwards, and you will "teleport" the GameObject with the inputMovement, but that does NOT affect physics.

If you don't want to use physics and are only interested in the collision, you should disable the physics simulation on the Rigidbody compontent.

So go ahead and set the Is Kinematic checkbox in your rigidbody to true.


And, if interested, the value affecting physics is the velocity of the Rigidbody:

GetComponent<Rigidbody>().velocity

You need a Rigidbody on one of the colliding objects, personally I put it on the player but use velocity to handle movement to make sure it works nice with collision physics. When using transform.translate your character will essentially teleport X units every frame, giving the illusion of movement but not actually physically moving. This can make things like teleporting through walls, etc, happen.

So I've told you how to solve your immediate problem, my suggestion is that you re-write your movement logic to use Rigidbody.velocity instead. Your movement seems simple so it's practically just replacing your transform.translate with this:

rb.velocity = joystickInput * speed;