So I’ve sort of got this code working but there are some annoying problems with it due to physics and so on and think this is the same sort of problem I remember Quill18 talking about in one of his FPS videos. I’ve got the code working fine but the moment I turn the camera all the positioning code and so on becomes borked.
Is there a way to fix the problem of your movement not righting itself when you’re using rigidbody and a third person camera? Or have I made a mistake here? In case you’re wondering what I’m doing I’m trying to create a standard flying vehicle but it seems I’ve accidentally made an uncontrollable hovercraft instead.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlightScript : MonoBehaviour {
float rotSpeed = 5;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.Space))
{
GetComponent<Rigidbody> ().AddForce (0, 30, 0);
}
if (Input.GetKey (KeyCode.Q))
{
GetComponent<Rigidbody> ().transform.Rotate (0, 0, 0.5f);
}
if (Input.GetKey (KeyCode.E))
{
GetComponent<Rigidbody> ().transform.Rotate (0, 0, -0.5f);
}
if (Input.GetKey (KeyCode.W))
{
GetComponent<Rigidbody> ().AddForce (0, 0, 30);
}
if (Input.GetKey (KeyCode.S))
{
GetComponent<Rigidbody> ().AddForce (0, 0, -30);
}
MouseRotate ();
}
void MouseRotate ()
{
float rotX = Input.GetAxis ("Mouse X") * rotSpeed * Mathf.Deg2Rad;
float rotY = Input.GetAxis ("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround (Vector3.up, rotX);
transform.RotateAround (Vector3.left, rotY);
}
}
If you want to test this, be sure to constrain all rotational axis on your rigidbody, should just work with a standard cube actually.
You are not applying force in the forward direction of your object. You are applying 30 force with respect to the z axis.
Here are some changes I made and it seems to work, but I am not sure what you are going for.
float rotSpeed = 5;
//This is added to replace you "Magic 30's within the add force parameters"
//Additionally since this is suppose to fly a ship, you may want to increase or decrease the applied
//Force based on the mass of the ship.
public float MaxSpeed = 10;
private Rigidbody _RigidBody;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
//Cache the Rigidbody component
//Because the GetComponent<Rigidbody>() is looking up the component everytime you call it
_RigidBody = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
Vector3 up = transform.up; //This gets the up direction of the object
_RigidBody.AddForce( up * MaxSpeed );
}
if (Input.GetKey(KeyCode.Q))
_RigidBody.transform.Rotate(0, 0, 0.5f);
if (Input.GetKey(KeyCode.E))
_RigidBody.transform.Rotate(0, 0, -0.5f);
if (Input.GetKey(KeyCode.W))
{
Vector3 forward = transform.forward; //This gets the foward direction of the object
_RigidBody.AddForce( forward * MaxSpeed );
}
if (Input.GetKey(KeyCode.S))
{
Vector3 forward = transform.forward; //This gets the foward direction of the object
_RigidBody.AddForce( forward * (-MaxSpeed) );
}
MouseRotate();
}
void MouseRotate()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed; //Not sure why you needed or wanted to convert to radians
float rotY = Input.GetAxis("Mouse Y") * rotSpeed;
transform.Rotate(Vector3.up, rotX);
transform.Rotate(Vector3.left, rotY);
}
I did disable all the rotations in the component, as well as the gravity.
Sorry, I should explain in a bit more detail, I’m creating flying vehicles a bit like the banshee in Halo and so on, bear with me, I got the rotation code which seemed to make sense here. The code is just a bit of mess I think because I’m working it out still myself.
What I want is to be able to use the vehicle though in a space sim manner like in Freelancer and space sims generally, where you can use the mouse to mouse to freely rotate your ship however the problem comes I think from me wanting to have some realistic force to the engines the gameobject will keep travelling in the direction that I originally pointed it non-stop and you can’t adjust it with your mouse.
I actually thought of a game just now that has the exact type of movement which should make things clearer for you.
Note how in this game even though the ship is still going in the direction the force was applied it still adjust itself and slows down, I wonder if that’s just all I’ve missed.
I’m in no rush or anything for this one but if it’s not too much of a bother, I was just looking through tutorials and ran into a wall, but what I’ve got planned is a little spaceship model you can get in and out of and fly around like in Halo: Combat Evolved. There’s ground tutorials for vehicles and so on but I haven’t yet found any space sim style ones, all of the other flight sim stuff and third person use the complicated Unity packages which I think have too much code in them for something simple and I want to start with the basic stuff first.
Ah here we go found Halo footage of people messing around with the Banshees.
Here is a link to my google drive. It contains the project file for what I have so far. Trying to get the camera to work correctly, but it is functional so far.
The settings in the rigidbody component can achieve some of the functionality that you would like. Atleast, from what I have seen from the videos. The drag field will lower the movement over time - the higher the faster. I think by default it is set to 0. The angular drag works similarly, but when the object is rotating.
lol oh wow, you got that all set up in a day? That’s pretty impressive, that’s pretty much the type of thing I was going for though so thanks, the rest is going to be about tweaking, man I really want to get some AI skirmishes set up in my shooter I have planned because that always makes games more fun, got to stay focused though.
Oooo, Quaternion.Slerp, got a new thing to look up that I didn’t know about.
Yeah, I have finals coming up, but after next week I should have a lot more free time to refine it.
I may just setup the camera to move and have the ship ‘mimic’ the camera. Not completely sure where it will end up. The barrel roll ‘q’ and ‘e’ don’t work anymore because, if i remember, the ‘q’ and ‘e’ keys are rotating the ship, but the ship is rotating with the camera - conflict…
Unfortunately, I cannot be much help on the AI front. I mean besides having an enemy attack you when you come close, but evasive maneuver’s and having a dogfight is currently beyond me.
Couldn’t Help myself. I updated the google drive file. The ship flies nicer. However, If you play with it for a bit, the camera will mess up and then the rotation becomes inverted.
That sounds like an issue like with when you’re making an FPS and you need to restrict the angle the camera can move at? Could be worth clamping the rotation somehow, will see if I can adapt the FPS code to this and maybe that will work.
Edit: Just realised that clamp might be the wrong option here because of course we actually want the camera to be able to rotate a full 360 degrees as we’re in a 3D space, hmmm, going to search around if no one posts anything up.
The script I have on the camera now was taken from another post. Modified it for the barrel roll. I have yet to debug the script to see exactly is going on before and after the camera gets funky.
May have to launch some games and take some notes on how the camera feels, and see if there are any restrictions.
It would be nice if we could pop this in and off we go. =D
LOL! I’ve set you off thanks to those older gameplay videos haven’t I? Avarion is the only recent game really I’ve seen aside from a few space sims that use that type of movement anymore. I’ve been thinking more about the rotating problem which seems to be the biggest issues and it definitely seems to be a gimbal lock problem, not that much on it though I’ve seen yet, so maybe someone else could chime in that knows more.
I was thinking we could clamp it by 360 degrees or something so the camera couldn’t go negative and flip over but I’m not sure that would work because then you’d only be able to rotate a certain amount and you’d be stuck just going forward.
Videos are nice but, It would be better to play with the camera myself. Those videos are about the game. I want to play with the camera and see how it works and feels.
I think that the camera will need to be clamped, because I don’t recall being able to rotate the camera 360 degrees on what would be the x or z axis. I think what happens is that if you rotate above or below the ship it stops at say 90 degrees, but you can rotate 360 on the y. Gimbal lock shouldn’t be an issue. I may be mistaken but I think quaternions mitigate the risk of gimbal lock.
May just start from scratch by rotating on one axis at a time - break the problem up into small pieces. This way we could see the rotational values in the inspector, and what happens to each axis when we move the mouse in a specific direction.