I’m a bit stuck… I want to make my spaceship roll to the left and to the right (on the z-axis I think, with the usage of Q and E), sorta like this: Do A Barrel Roll! - YouTube But I don’t really know how to go about this… I tried something with tilting, which doesn’t work at all… plus if you look up and tilt then my spaceship goes ape shit!
using UnityEngine;
using System.Collections;
public class FirstPersonController : MonoBehaviour {
public float movementSpeed;
public float mouseSensitivity;
private float upDownSpeed;
//public float rolling;
//CharacterController cc;
Rigidbody rb;
void Start() {
Screen.lockCursor = true;
//cc = GetComponent<CharacterController>();
rb = GetComponent<Rigidbody>();
}//start end
void FixedUpdate() {
//Rotation
float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
transform.Rotate(0, rotLeftRight, 0);
float rotUpDown = Input.GetAxis("Mouse Y") * mouseSensitivity;
transform.Rotate(-rotUpDown, 0, 0);
//Roll //doesn't work right at the moment!
//transform.Rotate(0,0,Input.GetAxis("Horizontal") * - tilt - transform.eulerAngles.z);
//Movement
float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
//Up and Down
if(Input.GetKey("space")) {
upDownSpeed = movementSpeed * Time.deltaTime;
} else {
if(Input.GetKey("left ctrl")) {
upDownSpeed = -movementSpeed * Time.deltaTime;
} else {
upDownSpeed = 0.0f;
}
}
//(left/right, up/down, forward)
//Vector3 speed = new Vector3(sideSpeed,0,forwardSpeed);
Vector3 speed = new Vector3(sideSpeed,upDownSpeed,forwardSpeed);
speed = transform.rotation * speed;
//cc.Move(speed * Time.deltaTime);
rb.velocity = movementSpeed * speed * Time.deltaTime;
}//update end
}//class end
It shouldn’t be much different than turning left/right or up/down. Line 29 would be something like:
rollLeftRight = Input.GetAxis("Roll") * tilt;
transform.Rotate(0, 0, rollLeftRight);
You are trying to use the current rotation in your commented out code. You didn’t need to do that for the X and Y rotation, therefore you don’t need to for Z rotation .