- Home /
How do I make a character controller "ride" a rocket?
Hi there,
I'm a bit of a Unity3D novice and could really use some pointers on a game that I'm building. It is a simple FPS game with one weapon: a really big rocket launcher. The character controller has within it a gun object that instantiates a rocket on left mouse click, and the rocket prefab has Constant Force on it so it flies forward. What I want to do is make it to on right mouse click, a rocket is instantiated and the player object is attached to it, with the FPS controls inactive. The point being that if the player clicks alternate-fire, they are able to ride on a rocket. Steering the rocket is done by moving the mouse. The player can jump off at any time and fall to the terrain, while the rocket continues on its trajectory.
So far, I've made it so when the game starts, a prefab (named Player) with the FPS controller in it is instantiated. Player has a Gun object (in the Mouselook) which has the script that instantiates rockets on mouse clicks, per the old Unity FPS tutorial. Movement controls and shooting rockets works fine. Just for kicks, I've also made a separate object with a capsule, camera, and mouselook on a Rocket object, with the mouse steering working. However, I don't think the "walking" player and the "flying" player modes should be separate objects. Rather, it seems to me like the logical way would be to have the Player prefab instance always remain, no matter if it was walking around with the FPS controls or sitting on a Rocket.
So, my main question is: How do I make the Player object physically attach itself to an instantiated Rocket object? I'm assuming I would update the Player's position with the Rocket's, but how do I refer back to the Player object from the script in the Rocket object?
Also: How do I disable the FPS controller (so they can't move with the arrow keys or jump) when a Player is riding on a Rocket?
Finally: How would I make the Player object's mouselook change the direction of the instantiated Rocket when the player is flying?
My apologies for loading this request with a lot of questions, and my general newbishness. I would genuinely appreciate any insight you guys could give me into these seemingly simple problems.
Thank you!
P.S. I originally posted this in the Unity Forums before I realized this might be a better place for it. Sorry for the cross-post!
Answer by SYNYST3R1 · Sep 26, 2011 at 01:26 AM
Have you tried making the player object a child of the rocket object? Not sure if that will work since I haven't done anything like that, but the position of the player will be that of the rocket if you make it a child object. You could disable the renderer then enable it when you want the rocket. That would be a really simple way if it works.
To disable a script you do something like this GetComponent("FPSController").enabled = false; then set it to true when you want it enabled.
You could make a call to the mouselook script using GetComponent and when you move the mouse x or y the rotation or position of the rocket changes. I believe there is two mouselook scripts one for x and one for y. Or you could do something like this without using the mouselook script in the update function/method of course
float speed = 1.0; transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) Time.deltaTime speed);
Your answer
Follow this Question
Related Questions
Rockets bending around enemies? 1 Answer
Fixing timestep to lower value showing wrong FPS 0 Answers
Why can't I shoot my bullet down? 1 Answer
Gravity only rigidbody 1 Answer
Limit & Scaling Translation Rate. 0 Answers