- Home /
How to move player on a moving vehicle
Hello.
I'm working on a "Sea of thieves like", and I have a problem.
My ship is controlled by forces added to its rigidbody, to make its movement with some inertia. My player also have a rigidbody, so it's using gravity, velocity etc...
When the player is on the ship, I want the player to be moving with the ship, and moving freely on the ship ( go on the bridge, in its room, etc etc...).
So, at the moment, I'm adding ship's rigidbody.velocity to the player's rigidbody, and the player is moving as I want.
Now my problem is, when the ship start turning, the player don't follow its rotation.
[CODE]
Here is my script:
public class ControllerFPS : MonoBehaviour {
Rigidbody rb;
[SerializeField] float velocity = 2.0f;
[SerializeField] float angularVelocity = 360.0f;
[SerializeField] float angularVelocityVertical = 100.0f;
[SerializeField] float jumpVelocity = 10.0f;
GameObject gBoat;
Rigidbody rBoat;
float deltaRot;
float boatRot;
float currentRotation = 0.0f;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
Cursor.lockState = CursorLockMode.Locked;
gBoat = GameObject.Find("Bateau");
rBoat = gBoat.GetComponent<Rigidbody>();
boatRot = gBoat.transform.rotation.y;
}
// Update is called once per frame
void Update () {
Vector3 tmpVelocity = Vector3.zero;
float desiredVelocity = velocity;
tmpVelocity += transform.forward * Input.GetAxis("Vertical") * desiredVelocity;
tmpVelocity += transform.right * Input.GetAxis("Horizontal") * desiredVelocity;
tmpVelocity = Vector3.ClampMagnitude(tmpVelocity, desiredVelocity);
if (Input.GetButtonDown("Jump"))
{
tmpVelocity.y = jumpVelocity;
}
else if (Input.GetButtonDown("Crouch"))
{
tmpVelocity.y = -jumpVelocity;
}
else
{
tmpVelocity.y = rb.velocity.y;
}
rb.velocity = tmpVelocity + rBoat.velocity;
//rb.velocity = tmpVelocity;
deltaRot = gBoat.transform.rotation.y - boatRot;
boatRot = gBoat.transform.rotation.y;
transform.Rotate(Vector3.up, Input.GetAxis("Mouse X") * Time.deltaTime * angularVelocity + deltaRot);
//transform.eulerAngles = transform.eulerAngles + new Vector3(0.0f, deltaRot, 0.0f);
//transform.localEulerAngles = transform.localEulerAngles + new Vector3(0.0f, deltaRot, 0.0f);
currentRotation += Input.GetAxis("Mouse Y") * Time.deltaTime * angularVelocityVertical;
currentRotation = Mathf.Clamp(currentRotation, -85.0f, 85.0f);
Camera.main.transform.localEulerAngles = new Vector3(-currentRotation,0,0);
}
}
[/CODE]
As you can see, I've tried some things, but these don't work.
Thanks for reading me, and waiting for help! Erwange
$$anonymous$$ake the ship a parent of your player when hes on board and un-parent the ship when he gets off
But I would even un-parent when the player jumps on the ship for example and re-parent when he lands on the ship, but always parented when he is grounded on the ship
I've tried that, but the rigidbody children of rigidbody made some unexpected behaviour
Your answer
Follow this Question
Related Questions
How to add gravity without a rigidbody 2 Answers
Problem with movement 0 Answers
Multiple Cars not working 1 Answer
Throwing a Spear 1 Answer