I have a tank that is supposed to move up a hill. I would like the tank to move parallel to the slope.
This problem has to do with the way I am rotating the tank, which is by transform.forward.
Vector3 newCoords = camera.TransformDirection(moveDirection.x, 0, moveDirection.y);
transform.forward = new Vector3(newCoords.x, 0f, newCoords.z);
moveDirection is the Vector2 from the controller's joystick. camera.TransformDirection is basically making the forward of the tank, the forward of the camera. I'm not exactly sure, but transform.forward is somehow resetting one of the axies, which doesn't allow it on the surface.
My question is how do I rotate the tank using transform.forward while keeping the tank parallel to the slope at all times?
If you need any clarification please ask. Thank you for reading, help would be greatly appreciated!
CodePudding user response:
You can try two different solutions:
Use a rigidbody and you don't have to do nothing.
You probably have to calculate angle between the tank and the ground. For doing this, you have to use a raycast (or spherecast,boxcast,...):
RaycastHit slopeHit;
if(Physics.Raycast(transform.position, Vector3.down, out slopeHit, raycastLength))
{
float angleBetween = Vector3.Angle(slopeHit.normal, Vector3.up);
}
Try implement it and use angle returned for modify the angle of your tank.
CodePudding user response:
Thanks for your reply!
I managed to solve it thanks to the raycast, and this golden post right here which details how to make transform.forward and transform.up cooperate.