Vector3 and transform.rotation?
Hi,
I'm trying to do a simple reset players position, I've got it all working apart from the players rotation. I'm trying to use a vector3 but I keep getting this error "OutOfZone.cs(24,34): error CS0029: Cannot implicitly convert type UnityEngine.Vector3' to
UnityEngine.Quaternion'"
public class OutOfZone : MonoBehaviour {
public Transform Player;
public Vector3 coOrds;
public Vector3 view;
public AudioClip fallSound;
public AudioClip goBackSound;
void OnTriggerEnter (Collider col)
{
StartCoroutine(outZone());
}
IEnumerator outZone()
{
CameraFade.FadeOutMain();
audio.PlayOneShot(fallSound);
yield return new WaitForSeconds(2);
audio.PlayOneShot(goBackSound);
Player.transform.position = coOrds;
Player.transform.rotation = view;
yield return new WaitForSeconds(1);
CameraFade.FadeInMain();
}
}
What do I use to get this to work?
Answer by CHPedersen · Mar 13, 2013 at 04:59 PM
The problem is pretty much exactly what the error says. :) In this line:
Player.transform.rotation = view;
You are attempting to set the rotation to the variable view, but view is of type Vector3, and the rotation of a Transform is of type Quaternion. Those types are incompatible - you can't set it like that. You have to either set the rotation using a Quaternion you create with the Quaternion constructor directly, or by creating a quaternion using one of the static methods in class Quaternion.
For example, since you're storing view as a Vector3, maybe I can assume you're actually storing the degrees of rotation around x, y and z in view.x, view.y and view.z? In that case, you can set your rotation like this:
Player.transform.rotation = Quaternion.Euler(view.x, view.y, view.z);
Or you can just set it like:
Player.transform.eulerAngles = view;
Thanks, Didnt know what a quaternion was since i've never seen it before in this. But that worked fine.
It's a matrix used to store rotations in a way that avoids Gimbal lock. $$anonymous$$ore on that on Wikipedia. If the reply helped you, please consider marking it correct. :)
Answer by KingKong320 · Jun 25, 2019 at 04:41 AM
i have tried eulars is vector3 type. transform.rotation = Quaternion.Euler (eulars.x, eulars.y, eulars.z); but it gives error Assertion failed on expression: 'CompareApproximately(SqrMagnitude(result), 1.0F)' UnityEngine.Quaternion:Euler(Single, Single, Single).
Please help me out.
Your answer
Follow this Question
Related Questions
Help with raycast for for wall detection 1 Answer
How to rotate the player towards a Vector2? 1 Answer
I want to make a script that causes the directional light to stop at 180 degrees. 0 Answers
Problem getting Vector3.Slerp to rotate in the correct direction in specific situation 0 Answers
Searching a function to get the rotation of one Vector3 in relation to another 1 Answer