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. Dismiss Notice

Quaternion in Unity 5

Discussion in 'Scripting' started by FranklyGaming, Mar 8, 2015.

  1. FranklyGaming

    FranklyGaming

    Joined:
    Mar 8, 2015
    Posts:
    7
    Hey guys just started up with Unity 5 (and Unity as a whole for that matter) and was wondering how to use Quaternion for one of the project examples. (http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player) At the end of this video they use a Quaternion to help tilt the space ship, but unity is not excepting my code. For rigidbody I have just been using GetComponent<Rigidbody>() as a fix (I know this might not be the correct fix an answer to this would help as well) but this method is not working for Quaternion. How should i be tackling this problem?

    Here is my code so far:

    using UnityEngine;
    using System.Collections;

    [System.Serializable]
    public class Boundary {
    public float xMin, xMax, zMin, zMax;
    }

    public class PlayerController : MonoBehaviour {

    public float speed;
    public float tilt;
    public Boundary boundary;

    void FixedUpdate() {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    GetComponent<Rigidbody>().velocity = movement * speed;

    GetComponent<Rigidbody> ().position = new Vector3 (
    Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
    0.0f,
    Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    );
    GetComponent<Rigidbody>().rotation = GetComponent<Quaternion>().Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity * -tilt);
    }
    }
     
  2. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
  3. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Perhaps try something like this :
    Code (CSharp):
    1.     private void FixedUpdate() {
    2.         float moveHorizontal = Input.GetAxis("Horizontal");
    3.         float moveVertical = Input.GetAxis("Vertical");
    4.  
    5.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    6.         GetComponent<Rigidbody>().velocity = movement * speed;
    7.  
    8.         GetComponent<Rigidbody>().position = new Vector3
    9.             (
    10.             Mathf.Clamp(
    11.                         GetComponent<Rigidbody>().position.x,
    12.                         boundary.xMin,
    13.                         boundary.xMax),
    14.             0.0f,
    15.             Mathf.Clamp(
    16.                         GetComponent<Rigidbody>().position.z,
    17.                         boundary.zMin,
    18.                         boundary.zMax)
    19.             );
    20.  
    21.         GetComponent<Rigidbody>().rotation = Quaternion.Euler(
    22.                         0.0f,
    23.                         0.0f,
    24.                         GetComponent<Rigidbody>().velocity.x * -tilt
    25.             );
    26.     }
     
  4. TEBZ_22

    TEBZ_22

    Joined:
    Jan 28, 2014
    Posts:
    37
    I have problems with rotating rigidbody in Unity5.
    Rotating the transform worked for me.
    I have no idea why (and in general what I'm doing:) )
     
    Last edited: Mar 8, 2015
  5. plasmabazooka

    plasmabazooka

    Joined:
    Mar 8, 2015
    Posts:
    70
    Why do you what to rotate rigidbody instead of transform?
    I suppose for rigidbody it is better to AddForce or AddForceAtPosition if you want to add some rotation.
     
  6. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    @TEBZ_22, @plasmabazooka,
    The tutorial repositions and rotates the Rigidbody, for what that's worth .. and the OP is just following the tutorial.
    I s'pose the question should be addressed to the tutor :)
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Yes that's correct, though you should cache the reference once in start
    Code (CSharp):
    1. Rigidbody rb;
    2.  
    3. //start
    4. rb = GetComponent<Rigidbody>();
    5.  
    6. //elsewhere
    7. rb.velocity = ...
    For your error, ksub has the answer, you missed the velocity".x" in the quaternion
     
  8. FranklyGaming

    FranklyGaming

    Joined:
    Mar 8, 2015
    Posts:
    7
  9. FranklyGaming

    FranklyGaming

    Joined:
    Mar 8, 2015
    Posts:
    7
    Thanks a lot this worked perfectly! One question though. Isn't GetComponent hard on the CPU, I.E. shouldn't I be using a different form in order to properly keep using Rigidbody without using too much CPU power? Or is this just how it works in Unity 5?
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    No, GetComponent isn't hard on the CPU.

    --Eric
     
    FranklyGaming likes this.
  11. plasmabazooka

    plasmabazooka

    Joined:
    Mar 8, 2015
    Posts:
    70
    You can take it one time and store a result to a private property of your class. You will get link to the component and don't need to call GetComponent more.
     
  12. TEBZ_22

    TEBZ_22

    Joined:
    Jan 28, 2014
    Posts:
    37
    -
    I Acualy tried the "Stelth" tutorials, using Unity5, and there the player have Rigidbodys FreezRotation(x;y;z) set, as stated in the tutorial. Then I had to rotate the Transform instead of the Rigidbody to make it work. If someone knows why..?