x


How to realize a moving, rotating Spaceship with a moving FPS-like Charakter inside?

First, sorry for my bad english. ;)

Okay, I got this:

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.

more ▼

asked Mar 10 '11 at 04:24 PM

Robert Sachse gravatar image

Robert Sachse
1 1 1 4

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Solved the problem my self with 1 easy trick.

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.

more ▼

answered Mar 11 '11 at 09:16 PM

Robert Sachse gravatar image

Robert Sachse
1 1 1 4

(comments are locked)
10|3000 characters needed characters left

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)

more ▼

answered Mar 10 '11 at 04:34 PM

AngryOldMan gravatar image

AngryOldMan
2.6k 12 21 47

I want to make a multiplayer game in which several players can act together on 1 ship. ^^ Therefor I need to calculate where the ship is moving while the players are inside. The problem with activating kinematic when the player is inside a ship is simple, that he will not collide correctly with some other kinematic objects like doors if the ship is moving fast. For this reason I need continious dynamic collision detecting and this is only working, if 1 object is nonkinematic, which is the player. So thanks for your answer, but that's not the solution for this complex issue.

Mar 10 '11 at 04:45 PM Robert Sachse

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?

Mar 10 '11 at 04:47 PM AngryOldMan

Well, other players or NPCs in other Ships in Space should be able to see the ship moving, collide with it, fire on it, etc... and this calculation needs to go on while the player is inside. So I thought the best solutions is just to put him realy inside the ship and just let the game run the scene, switch cameras&controls when flipping insideout or otherwise. Maybe there is a better solution to this. I also have a script that sets the ship to the floating origin and moves the world, but there are physics and camera issues, so I just use it after 1000 units in space to prevent spartial jitter.

Mar 10 '11 at 05:49 PM Robert Sachse

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?

Mar 10 '11 at 07:41 PM AngryOldMan
(comments are locked)
10|3000 characters needed characters left

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);

...and then I could do:

transform.position = transform.root.position+newVector;

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).

Any ideas?

more ▼

answered Mar 10 '11 at 06:13 PM

Robert Sachse gravatar image

Robert Sachse
1 1 1 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x810
x49
x30
x27

asked: Mar 10 '11 at 04:24 PM

Seen: 1340 times

Last Updated: Mar 10 '11 at 04:24 PM