Hello to the person who see that.
I am new to creating games .
I need a code or something to help me move a ball with an touch joystick
. Here all easy.
the problem is that if i want to move the camera to 90 degrees to the right and after that I am move the joystick up the ball is going to the left . if you know what i mean…
by the way. i am using “add force”
and this is the code i use for the ball:
{
public float speed = 10f;
public VariableJoystick Jk;
Rigidbody Rb;
// Start is called before the first frame update
void Start()
{
Rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
private void FixedUpdate()
{
Vector3 move_dir = Vector3.forward * Jk.Vertical + Vector3.right * Jk.Horizontal;
Rb.AddForce(move_dir * speed * Time.deltaTime);
}
}
Whenever something doesn’t rotate with something else, it’s because you haven’t told it to do so. So the ball still thinks “North” is up, while you rotated the camera to the “East” which is now camera up. Easiest way to handle this(if you plan to keep the camera with the ball) is make an empty GameObject, and child the ball AND the camera to it(meaning you grab the ball and camera on the left side inspector and drag them into the empty game object). So when empty object rotates, so does the ball and camera with it. Now this might not give you the best results, depending on how fancy you want your camera to be. In more defined cases, you need to do math, and set transform.positions
and transform.rotations
down to the float a = 0.000001f
in severe cases. But if I were you, I’d search youTube on Quaternions and Euler Angles(make sure you have coffee, notepad, and headache medicine ready) :)