Need help with Quaternion rotation + 3rd person mouse follow (601002)

Hi, I’m trying to make a game kinda like Star Wars Rogue Squadron mixed with a pinch of Descent. But I’ve some issues…

#1
I can’t make Quaternion rotation work :frowning: I’ve never tried this before, so I honestly don’t really know how to make this work… I’ve already looked at UnityScriptReference and it helped me at least fix two lines of the code, but I’m not sure what to do about Quaternion tempRot = target.transform.rotation + rotation; it says that Operator ‘+’ cannot be applied to operands of type ‘Quaternion’ and ‘Quaternion’

using UnityEngine;
using System.Collections;

public class SmoothCamFollow : MonoBehaviour {

public GameObject target;
public float smooth;
public Vector3 position;
public Quaternion rotation;

void FixedUpdate () {
Vector3 tempPos = target.transform.position + position;
transform.position = Vector3.Lerp(transform.position, tempPos, Time.deltaTime * smooth);

transform.LookAt(target.transform);
}
}

#2
How to make a space ship fly after a crosshair? I’m trying to make my game like this:

but I can’t make my space ship fly in the direction that I point my mouse towards (following the crosshair). I’ve tried to use different scripts but I can’t make anything work… I’ve been stuck at this for a while now :confused:

using UnityEngine;
using System.Collections;

public class LookAtMouse : MonoBehaviour {

public GameObject player;
public float speed;

void FixedUpdate() {
Plane player = new Plane(Vector3.up, transform.position);

Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

float hitdist = 0.0f;
// If the ray is parallel to the plane, Raycast will return false.
if (player.Raycast(ray, out hitdist)) {
// Get the point along the ray that hits the calculated distance.
Vector3 targetPoint = ray.GetPoint(hitdist);

Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);

transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
}

-------------------- and I’ve also tried this, which doesn’t work either -------------

using UnityEngine;
using System.Collections;

public class CamLook : MonoBehaviour {
public float sensitivity = 1f;

public float minimumY = -60f;
public float maximumY = 60f;

private float rotationY = 0f;
private float rotationX = 0f;

void Update() {
rotationX = transform.localEulerAngles.y + Input.GetAxis(“Mouse X”) * sensitivity * Time.deltaTime;

rotationY += Input.GetAxis(“Mouse Y”) * sensitivity * Time.deltaTime;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
}

To combine Quaternion rotations, you multiply them. Order matters here, so be careful.

Quaternion tempRot = target.transform.rotation * rotation;

As for flying towards the crosshair, I imagine you don’t. I’d assume the crosshair is just a projection of your current rotation relative to flying straight ahead.

Really? lol, I was so stuck on that shit >_> but the rotations doesn’t do anything, it doesn’t rotate the cam at all, regardless of what values i put into it :confused:

As for the crosshair thing, what should I do instead then? Add rotation or something to the ship?
This is why my playercontroller looks like atm:

  • using UnityEngine;
  • using System.Collections;
    • public class PlayerController : MonoBehaviour
  • {
  • public float speed;
  • public float tilt;
    • private float nextFire;
    • //movement
  • void FixedUpdate ()
  • {
  • float moveHorizontal = Input.GetAxis(“Horizontal”);//left & right
  • float moveVertical = Input.GetAxis(“Vertical”);//back & forward
    • Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
  • GetComponent().velocity = movement * speed * Time.deltaTime;
    • //tilting
  • GetComponent().rotation = Quaternion.Euler
  • (
  • 0.0f, //x
  • 0.0f, //y
  • GetComponent().velocity.x * -tilt //z
  • );
    • }
  • }

I guess I’ve to make the ship fly after my mouse cursor or something? I don’t really know how I should approach this…

If only there was a way to clean up the mess and make the code readable
1 Like

You need to set the velocity relative l to the ship’s forward after rotation to make it move properly.

I whipped this up:

And here’s the code:

public class RogueSquadron : MonoBehaviour
{
   protected Rigidbody rigidBody;

   protected float horizontalAxis;
   protected float verticalAxis;

   protected void Start()
   {
     rigidBody = GetComponent<Rigidbody>();   
   }

   protected void Update()
   {
     horizontalAxis = Input.GetAxis("Horizontal");
     verticalAxis = Input.GetAxis("Vertical");   
   }

   protected void FixedUpdate()
   {     
     rigidBody.MoveRotation(rigidBody.rotation * Quaternion.Euler(40.0f * verticalAxis * Time.deltaTime, 0f, 40.0f * -horizontalAxis * Time.deltaTime));
     rigidBody.velocity = rigidBody.transform.forward * 20.0f;
   }
}

It’s obviously very, very rough and Rogue Squadron has a lot of nuance to it. It’s not something we can just solve on the forum in a couple of posts, you’re going to have to put some blood, sweat and tears to get it anywhere close to Rogue Squadron’s level, but this will hopefully get you started in the right direction.

Hi GroZZleR,

Sorry about the extremely late reply. Thanks for the hand but this still doesn’t fix my issues with having it flow after the mouse… I’m not sure how else to express it… I want it to follow the crosshair like you would do in an FPS game. Controlling the direction it’s flying with WASD is pretty close to impossible… but I can’t make it work with the usage of the mouse…

It will look better when you detach the camera and make it smooth follow the ship.

Just for anyone that is interested, I reached the desired result like this:
Thanks for the assist though :slight_smile:

using UnityEngine;
using System.Collections;

public class FirstPersonController : MonoBehaviour {
   
    public float movementSpeed;
    public float mouseSensitivity;
   
    void Start() {
        Screen.lockCursor = true;
    }
   
    void Update() {
       
        //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);
       
        //Movement
        float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
        float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
       
                                 //(left/right, up/down, foward)
        Vector3 speed = new Vector3(sideSpeed,0,forwardSpeed);
       
        speed = transform.rotation * speed;
       
        CharacterController cc = GetComponent<CharacterController>();
       
        cc.Move(speed * Time.deltaTime);
       
    }
   
}