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 'Getting Started' 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);
    }
    }
     
    Last edited: Oct 10, 2015
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    I don't have much time, but two quick suggestions for you: (1) use code tags around your code to make it readable (easiest way: use the "Insert Code" button, to the left of the floppy disk icon); (2) you compose quaternions with *, not with +.

    Good luck!
     
  3. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Which is to say, if you have a Quaternion representing a rotation, and you want to rotate that even further, you multiply the delta on top of it. So for example, if you have a Quaternion.Euler(0, 0, 20) and you want to rotate it so that you end up with a Quaternion.Euler(0, 0, 50), you just need to multiply a Quaternion.Euler(0, 0, 30) on top of it (50 - 20 = 30.) It's really that simple. Of course this doesn't only work for a single axis at a time.
     
    JoeStrout likes this.