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

why does this not work? how do i fix it?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletPosScript : MonoBehaviour
{

Vector3 rotation;
public GameObject AssultRifle;
void Start()
{

}

void Update()
{

rotation = new Vector3(AssultRifle.transform.rotation.x, AssultRifle.transform.rotation.y + 90, AssultRifle.transform.rotation.z);
transform.rotation = rotation;

}
}

Transform.rotation is a Quaternion, not a Vector3: Unity - Scripting API: Transform.rotation

Your compile error is probably telling you this!

Maybe you want transform.rotation = Quaternion.Euler(rotation);

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

If you want to set a Vector3 (euler) rotation, you will need to use eulerAngles or localEulerAngles instead of rotation/localRotation.

using UnityEngine;

public class BulletPosScript : MonoBehaviour
{
public GameObject AssultRifle;

Vector3 rotation;

void Update()
{
rotation              = AssaultRifle.transform.eulerAngles + new Vector3(0, 90, 0);
transform.eulerAngles = rotation;
}
}

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.

1 Like