Search Unity

Why can i not make transform.rotation to a vector 3 varieble?

Discussion in 'Scripting' started by Confused_Jonas, Jul 11, 2020.

  1. Confused_Jonas

    Confused_Jonas

    Joined:
    May 28, 2020
    Posts:
    47
    why does this not work? how do i fix it?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulletPosScript : MonoBehaviour
    6. {
    7.  
    8.     Vector3 rotation;
    9.     public GameObject AssultRifle;
    10.     void Start()
    11.     {
    12.        
    13.     }
    14.  
    15.    
    16.     void Update()
    17.     {
    18.  
    19.         rotation = new Vector3(AssultRifle.transform.rotation.x, AssultRifle.transform.rotation.y + 90, AssultRifle.transform.rotation.z);
    20.         transform.rotation = rotation;
    21.  
    22.     }
    23. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    It is always enlightening to go visit the documentation page for what you're having trouble with.

    Here are some other notes on the error you're probably seeing:

    Some help to fix "Cannot implicitly convert type 'Xxxxx' into 'Yyyy':"

    http://plbm.com/?p=263
     
  4. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    265
    If you want to set a Vector3 (euler) rotation, you will need to use eulerAngles or localEulerAngles instead of rotation/localRotation.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class BulletPosScript : MonoBehaviour
    4. {
    5.     public GameObject AssultRifle;
    6.  
    7.     Vector3 rotation;
    8.  
    9.     void Update()
    10.     {
    11.         rotation              = AssaultRifle.transform.eulerAngles + new Vector3(0, 90, 0);
    12.         transform.eulerAngles = rotation;
    13.     }
    14. }
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    It's all very confusing. The Inspector shows rotation as x,y, and z degrees, but it's LYING. As PraeterBlue notes, rotations are actually 4 incomprehensible numbers called a "Quaternion". Everyone (games, real stuff) uses them now since they're better at smoothy rotating between any angles. And by incomprehensible, I don't mean cosins and sins -- they're way worse than that.

    transform.rotation=Quaterion.Euler(x,y,z) works since it turns your x,y,z degrees into a proper rotation (a quaternion). If you mouse over transform.rotation, it even says "quaternion".

    The extra weird thing is that it turns the quaternion back into x,y,z degrees for the Inspector, which may be _different_ than you used. If you had rotation=Quaternion.Euler(270,0,190) Unity may decide that (-90,0,190) looks better in the Inspector. Even worse, they may be a 3rd set of values in your code. Your program can translate a rotation into x,y,z degrees with Vector3 xyz=transform.rotation.eulerAngles. That's actually a function call. It might give you (-90,0,-170), which is the same angle, but it thought 190 looked nicer as -170 this time.

    The end result is you can "input" x,y,z degrees into a rotation just fine, but you can't read them back (except for debugging) since they can jump around.
     
    PraetorBlue likes this.