Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help with Quaternion rotation + 3rd person mouse follow

Discussion in 'Scripting' started by johnniks, Oct 10, 2015.

  1. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    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 :( 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 :/

    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);
    }
    }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    To combine Quaternion rotations, you multiply them. Order matters here, so be careful.

    Code (csharp):
    1.  
    2. 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.
     
  3. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    Really? lol, I was so stuck on that S*** >_> but the rotations doesn't do anything, it doesn't rotate the cam at all, regardless of what values i put into it :/

    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:

    1. using UnityEngine;
    2. using System.Collections;

    3. public class PlayerController : MonoBehaviour
    4. {
    5. public float speed;
    6. public float tilt;

    7. private float nextFire;

    8. //movement
    9. void FixedUpdate ()
    10. {
    11. float moveHorizontal = Input.GetAxis("Horizontal");//left & right
    12. float moveVertical = Input.GetAxis("Vertical");//back & forward

    13. Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    14. GetComponent<Rigidbody>().velocity = movement * speed * Time.deltaTime;
    15. //tilting
    16. GetComponent<Rigidbody>().rotation = Quaternion.Euler
    17. (
    18. 0.0f, //x
    19. 0.0f, //y
    20. GetComponent<Rigidbody>().velocity.x * -tilt //z
    21. );
    22. }
    23. }
     
    Last edited: Oct 11, 2015
  4. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    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...
     
  5. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Code (CSharp):
    1. If only there was a way to clean up the mess and make the code readable
     
    LaneFox likes this.
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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:
    Code (csharp):
    1.  
    2. public class RogueSquadron : MonoBehaviour
    3. {
    4.    protected Rigidbody rigidBody;
    5.  
    6.    protected float horizontalAxis;
    7.    protected float verticalAxis;
    8.  
    9.    protected void Start()
    10.    {
    11.      rigidBody = GetComponent<Rigidbody>();  
    12.    }
    13.  
    14.    protected void Update()
    15.    {
    16.      horizontalAxis = Input.GetAxis("Horizontal");
    17.      verticalAxis = Input.GetAxis("Vertical");  
    18.    }
    19.  
    20.    protected void FixedUpdate()
    21.    {    
    22.      rigidBody.MoveRotation(rigidBody.rotation * Quaternion.Euler(40.0f * verticalAxis * Time.deltaTime, 0f, 40.0f * -horizontalAxis * Time.deltaTime));
    23.      rigidBody.velocity = rigidBody.transform.forward * 20.0f;
    24.    }
    25. }
    26.  
    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.
     
  7. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    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...
     
  8. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    It will look better when you detach the camera and make it smooth follow the ship.
     
  9. johnniks

    johnniks

    Joined:
    Oct 2, 2013
    Posts:
    26
    Just for anyone that is interested, I reached the desired result like this:
    Thanks for the assist though :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FirstPersonController : MonoBehaviour {
    5.    
    6.     public float movementSpeed;
    7.     public float mouseSensitivity;
    8.    
    9.     void Start() {
    10.         Screen.lockCursor = true;
    11.     }
    12.    
    13.     void Update() {
    14.        
    15.         //Rotation
    16.         float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
    17.         transform.Rotate(0, rotLeftRight, 0);
    18.        
    19.         float rotUpDown = Input.GetAxis("Mouse Y") * mouseSensitivity;
    20.         transform.Rotate(-rotUpDown, 0, 0);
    21.        
    22.         //Movement
    23.         float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
    24.         float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
    25.        
    26.                                  //(left/right, up/down, foward)
    27.         Vector3 speed = new Vector3(sideSpeed,0,forwardSpeed);
    28.        
    29.         speed = transform.rotation * speed;
    30.        
    31.         CharacterController cc = GetComponent<CharacterController>();
    32.        
    33.         cc.Move(speed * Time.deltaTime);
    34.        
    35.     }
    36.    
    37. }