- Home /
Rigidbody rotation with virtual joystick, weird behaviour
This is the game i'm working on. I have a really bad time trying to figure out a problem.
With this script attached, the tank moves properly, rotates according to the joystick direction, but.. when the rank tries to climb a ramp, it appears that it goes a bit through the ramp, before climbing and acting weird.
Also, when the tank goes off a cube, it falls, but stays a bit in the air. When I release the joystick, fells faster. Lots of weird rotations also occur when simply moving.
I assume the problem is in the code that calculates the rotation, as it modifies the x and z axis. But I have no idea, after hours spending searching tutorials, how to fix this. This is my first question on a website like this. Also, sorry for my bad english.
I attached a photo of the game so that u know what I mean with those ramps. Also, the script is attached below. Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MobileControls : MonoBehaviour {
public Joystick movementJoystick;
public int rotSpeed = 100;
public float moveSpeed = 10;
public Vector3 vec;
public Vector3 _centerOfMasss;
Rigidbody rb;
public float joyH = 0;
public float joyV = 0;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.centerOfMass = _centerOfMasss;
}
private void Update()
{
joyH = movementJoystick.Horizontal;
joyV = movementJoystick.Vertical;
TryThisToRotate();
}
private void FixedUpdate()
{
Vector3 movement = new Vector3(joyH * moveSpeed * Time.deltaTime, 0, joyV * moveSpeed * Time.deltaTime);
rb.MovePosition(transform.position + movement);
}
void TryThisToRotate()
{
if (joyH != 0f || joyV != 0f)
{
this.transform.localEulerAngles = new Vector3(this.transform.localEulerAngles.x, Mathf.Atan2(-joyH, -joyV) * Mathf.Rad2Deg, this.transform.localEulerAngles.z);
}
}
}
We must learn from you how the tank is configured. The description you give is recognized where any vehicle approaching an object seems to penetrate the object when the "wheels" or "tracks" haven't reached that object, like the ramp.
Without knowledge of your means of setting up the tank, all who help can only speak in generalities about that. It is a recognizable task upon which many hours have been spent learning. You can get help here, and you may find assets in the store designed to improve vehicle performance.
I've built a kind of $$anonymous$$iature monster truck (part of a robotics simulation, less of a game, more of a tool) which drives over objects and obstacles, so I recognize what you are describing. $$anonymous$$y general advice on that is if using wheel colliders as a vehicle design, they assume the track is smooth, and obstacles to climb, like a ramp, have very smooth geometric interfaces. They seem to operate like wheeled carts with very tiny wheels, where any obstacle causes major bouncing.
With all of that said, I must also advise that one either stick entirely with code manipulation of the object, or stick entirely with physics. $$anonymous$$ixing the two almost never works well - that is, direction alteration of the orientation (setting angles of the transform) while also letting physics impose gravity and handle collisions is problematic at best. Either drive by physics and steer, or drive kinetically and ignore physics.