- Home /
Camera relative rigidbody third person player movement
I have been trying to do this for the longest of times and still haven't found a solution. All I need is to do the following: when pushing upwards on the joystick, the player will move forward in the direction that the camera is facing (Camera follows the player but the rotation is fixed, only the user decides wether it should rotate it). The script should use AddForce since it's a non-kinematic Rigidbody and not MovePosition I have found multiple scripts and tried these scripts, but none takes into account what im trying to do:
Unity Standard Assets ( ThirdPersonCharacter.cs ) - Does exactly what I want respective to the camera relative movement, but the movement is based on Animation and not on AddForce.
RigidbodyFPSWalker (Well known script - look it up) - Doesn't take into account the camera.
Some_really_simple_script_that_just_uses_MovePosition.cs" - Doesn't take into account the camera, and shouldn't use MovePosition, this method should only be used for Kinematic RigidBodies.
Please any help would be apreciated Thank you in advance.
Answer by Serge144 · Aug 29, 2020 at 08:17 PM
I built this script from parts of differents scripts and it's working! I only have some questions regarding this script.
public class PlayerControllerRigid : MonoBehaviour
{
public float speed = 10.0f;
public float maxVelocityChange = 10.0f;
private new Rigidbody rigidbody;
public Joystick joystick;
void Awake()
{
rigidbody = GetComponent<Rigidbody>();
}
private void Update()
{
float horizontal = joystick.Horizontal;
float vertical = joystick.Vertical;
Vector3 input = new Vector3(horizontal, 0f, vertical);
Vector3 inputV2 = Quaternion.Euler(0, Camera.main.transform.eulerAngles.y, 0) * input;
if (inputV2 != Vector3.zero)
transform.rotation = Quaternion.LookRotation(inputV2, Vector3.up);
}
void FixedUpdate()
{
float horizontal = joystick.Horizontal;
float vertical = joystick.Vertical;
// Calculate how fast we should be moving
Vector3 camForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
Vector3 targetVelocity = new Vector3(horizontal, 0, vertical);
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity = vertical * camForward + horizontal * Camera.main.transform.right;
targetVelocity *= speed;
// Apply a force that attempts to reach our target velocity
Vector3 velocity = rigidbody.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
}
}
Can I somehow improve the rotation of the player on the Update method? I dont want any rotation parameters, I just want to know if maybe its better to use AddTorque since its a rigidbody.
The movement seems slightly jiggly (not as smooth as it should be), any idea why is that?.
Regarding 2), i researched and by changing the fixed timestep from 0.02 to 0.016 it fixed the jitter (Edit > Project Settings > Time > Fixed TimeStep). However I wanted to know if there's anything else in my code that is causing this jitter.