1 transform with an rigidbody attached to it without use of gravity in non-kinematic mode<-- that's my Spaceship
This transform has a collider (eg Boxcollider).
And I got a player, which has a transform, a collider and also a rigidbody without gravity in non-kinematic mode, so i can use continious dynamic collisiondetection, which i need, if the ship is moving fast.
Inside the Ship are some other parented transforms with kinematic rigidbodies and colliders like doors, to which the player should collide correctly, even on high speed.
Now the matter is, that the player wont follow the ship, even if I parent him to the ship because of the non-kinematic rigidbody which i need to have for correct collisions.
(Otherwise the collisiondection will fail on high speed due float inaccuracy.)
So I tried to "software parent" him with setting his position to the position of the spaceship + his local position.
That works as long as the ship is not rotated.
If the Ship rotates, I need to rotate the local distance positionvector of the player to the rotation of the ship and I don't have a clue how to manage that.
So, has anyone an idea to solve this "Player inside Ship"-problem?
I've allready written my own charaktercontroller to handle the situation inside the ship (gravity aligns to the ship, jumping as also, ...), but for this point I have no solution.
why does your ship need to move when the player is inside? if the player isn't visible you could just destroy him and assume control of the ship then instantiate him when your done controlling the ship. Or if you control your player while inside the ship but still want your ship to "move around" you could try moving the rest of the level around the static ship OR you could put animating textures in the windows to simulate outside movement.
Another fix if you definatly have to go down the route you described just code something like `rigidbody.isKinematic = true;` which activates when your player is parented to the ship and of course false when you unparent him (assuming you unparent him at all)
okay I understand that, so why does your ship need to move when people are on it? what about the 1st half of my reply?
so what about destroying (or hiding) the player after it gets into the space ship then instantiating it (or transform position then reveal) when players get out?
Here is some code I used in the script attached to the player:
var playerParent : GameObject;
function Awake () {
transform.parent=playerParent.transform;
}
function Update () {
if (lastPosition.magnitude==0)
{
lastPosition = transform.localPosition;
}
transform.position = transform.root.position+lastPosition;
}
Works fine, as well as the parent is not rotating.
To rotate lastPosition (the position of the player inside the ship) correct to the ship, I need the correct rotationangles of the ship.
Then I can rotate lastPosition (as playerDistance in this example) with this (:
var X : float;
var Y : float;
var Z : float;
Y += (playerDistance.y*Mathf.Cos(shipRotationAngles.x) - playerDistance.z*Mathf.Sin(shipRotationAngles.x));
Z += (playerDistance.y*Mathf.Sin(shipRotationAngles.x) + playerDistance.z*Mathf.Cos(shipRotationAngles.x));
X += (playerDistance.x*Mathf.Cos(shipRotationAngles.y) + playerDistance.z*Mathf.Sin(shipRotationAngles.y));
Z += (- playerDistance.x*Mathf.Sin(shipRotationAngles.y) + playerDistance.z*Mathf.Cos(shipRotationAngles.y));
X += (playerDistance.x*Mathf.Cos(shipRotationAngles.z) - playerDistance.y*Mathf.Sin(shipRotationAngles.z));
Y += (playerDistance.x*Mathf.Sin(shipRotationAngles.z) + playerDistance.y*Mathf.Cos(shipRotationAngles.z));
var newVector = Vector3(X,Y,Z);
So the question is, how to get the right angles?
transform.root.rotation is a quaternion which gives wrong angles if I want them to use with this script (real 360 needed).
Just added another empty gameobject to the ship and let this control the players position and rotation.
In update() of the real playerobject script I just set its position to the empty gameobject which is parented to the ship.
So easy, but it was hard to find out this solution.
Could you please share a script that you used for this ? Thanks.
I made player to be a child of gameobject, that is a child of a Ship. However, I cant move with my player, since in update method of playerscript, position is set to holdingObject's position. How did you solve this problem ?
okay I understand that, so why does your ship need to move when people are on it? what about the 1st half of my reply?
– anon61858128so what about destroying (or hiding) the player after it gets into the space ship then instantiating it (or transform position then reveal) when players get out?
– anon61858128