- Home /
Controll 3D Rigidbody Projectile with Joystick
Basically I shoot a 3D projectile, the camera then follows the projectile and what I wanted to do is to controll that projectile with the joystick.
Right now I have this on the FixedUpdate:
if (IsFollowingBall) {
Rb.AddForce(transform.TransformDirection(PlayJoystick.Direction)*100f);
}
The problem with this code is that, if I push the Joystick to the right (for example), the Ball will move to the right BUT at the worlds coordinates, which means that once the ball movement is aligned with the Right axis of the World it will keep following that Right Axis (thus moving forward on that axis now). What I wanted is for it to move to the Right but related to the ball itself.
Thanks in advance.
Answer by DenisIsDenis · Oct 30, 2021 at 03:49 AM
After testing your code, I found that the object is already moving relative to its local coordinates. After all, the TransformDirection()
function already translates the joystick vector into local coordinates. Most likely, your projectile is not rotated in the required direction. This can be done, for example, like this:
Rb.transform.forward = Rb.velocity;
my projectile has Torque applied to it so it is constantly rotation
Due to the rotation of the projectile, its local coordinates also rotate. Therefore, it is better to use the local coordinates of your camera, which is flying behind the projectile. Here's some sample code:
Rb.AddForce(Quaternion.Euler(0, cam.transform.localEulerAngles.y, 0) * moveVector); // cam is your camera
Your answer
Follow this Question
Related Questions
Adding player velocity to projectile not working 0 Answers
Why does the input not go back to zero when controls are released? 1 Answer
Need some help with firing projectile at player using Rigidbody Velocity 1 Answer
Increase velocity without changing trajectory 1 Answer
Convert rigidbody velocity to home in on target when it gets close 1 Answer