- Home /
Space Combat Simulator - Camera and Thrust/Roll
Hello there!
//TL;DR
So, I'm new to Unity and I'm trying to make a space combat simulator where the player pilots a fighter using the mouse to set the direction (pitch and yaw), two keys to increase or reduce trust (lets make it "W" and "S") and another two keys to "roll" the fighter (lets make it "A" and "D")...~
With that in mind, I've set up the main camera with a Smooth Follow script aimed at the Fighter, and a Mouse Look script in the fighter. But here is when the problem starts, because, the camera doesn't follows the rear of the craft when I move my mouse on the Y axis, but always follows when I move it on the X axis.
//End TL;DR
So, I've come here searching for help in two things:
How can I make the camera be always looking to the rear of the fighter?
How can I setup the thrust and roll for my fighter?
Thank you very much, and have a good day.
Answer by Lo0NuhtiK · Dec 30, 2011 at 01:49 AM
For the camera -> Take the smooth follow off of the camera, child the camera to the vehicle, then position the camera where you want it.
For the flight -> quick-code to get you started (UnityScript/Javascript) ->
@HideInInspector var myRigid : Rigidbody ; //used for caching this objects rigidbody var thrust : float = 100.0 ; var roll : float = 100.0 ; function Start(){ myRigid = rigidbody ; } function Update(){ FlyMeToTheMoonAndLetMePlayAmongstTheStars(); } function FlyMeToTheMoonAndLetMePlayAmongstTheStars(){
//roll control for left/right arrow keys, and/or 'a'/'d' myRigid.AddRelativeTorque(0,0, Input.GetAxis("Horizontal") (roll Time.deltaTime)) ;
//thrust control for up/down arrow keys, and/or 'w'/'s' myRigid.AddRelativeForce(0,0, Input.GetAxis("Vertical") (thrust Time.deltaTime)) ; } //NOTE : the "Input.GetAxis...." parts might need changed to a negative value, I can't remember what the default buttons are for negative/positive axis. //e.g. if you push left, but it rolls right, change it to " - Input.GetAxis.... "
Thank you very much the part of the Thrust is now beginning to look good, and the camera is now behind the fighter (the roll still ain't work, though)...
If you don't $$anonymous$$d I make another question is on how to smooth the camera turn a bit so that it makes a bit of angle from the fighter, but always keeps the thrusters focused?
Nope, reached to increase the value up to 1000000.0 and still no Roll...
So is there a way to keep the F-302 pointing at the mouse, and "Roll"?
Well I've found out someone with exactly this problem... $$anonymous$$aybe this would fix it?
http://answers.unity3d.com/questions/18534/rotating-local-z-axis-roll-on-a-mouselook-or-simil.html
Or is there a better way?
Idk lol go ahead and give it a shot. I just like trying to figure out the code myself to learn along the way. Trial and Error $$anonymous$$ches me more than copy/paste :D
Edit : For anyone else viewing this, shortly after posting the code above @JPB18 and I discovered that the mouse-look script interferes with the code when they're both being used. I deleted my part of the conversation because I don't like to leave a bunch of posted comments everywhere.
Your answer
Follow this Question
Related Questions
How to slow down a spaceship using C#? 1 Answer
How spaces works in unity? 1 Answer
Move in direction of mouse look 2 Answers
Uneven camera movement during roll 0 Answers
How can i center the camera to a moving object using space? 1 Answer