- Home /
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.
Answer by Robert Sachse · Mar 11, 2011 at 09:16 PM
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.
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 ?
Answer by AngryOldMan · Mar 10, 2011 at 04:34 PM
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)
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.
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?
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. $$anonymous$$aybe 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.
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?
Answer by Robert Sachse · Mar 10, 2011 at 06:13 PM
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?
Answer by Dynam1ke · Jun 09, 2014 at 08:56 AM
Im having the same issue as you had. could you please give more detail about how you resolved your issue? thanks in advance
Your answer
Follow this Question
Related Questions
Independently moving Rigidbody inside a moving Rigidbody 1 Answer
Detect if player is in structure (or in range) 1 Answer
Code working for tutorial guy but not for me. 1 Answer
Is it possible to open a unity game/application on an iphone/ipad inside a regular app? 2 Answers
How to create a hollow sphere world? 2 Answers