rotation/quaternion questions

I’m trying to write a script write a script that would re-set the rotation of an object to (0,0,0) and I cant find out how to do that. In the script reference page, I cant find the quaternion function that can edit it directly to (0,0,0). I think I can do it by multiplying the current rotation by -1 and then setting the Euler to that, but I cant find out how to do that. (right now I have Playercam.rotation = Quaternion.Euler(0,0,0); and in the variable section I have var Playercam : Transform; and in a awake function I have

if (Playercam == null  GameObject.FindWithTag("Player2")) 
Playercam = GameObject.FindWithTag("Player2").transform;

)

This:

Playercam.rotation = Quaternion.Euler(0,0,0);

Will work, but this:

Playercam.rotation = Quaternion.identity;

Is a little more straightforward.

I dont think you understand, I need to reset it, and Euler doesnt make it (0,0,0), it just adds or subtracts the values in the () argument list.

What do you mean by ‘reset it’?

reset the x,y,z values to 0 from any random values

Did you try:

Playercam.rotation = Quaternion.identity;

?

As far as I can tell, that’s what you’re looking for. If it’s not, perhaps you could clarify why it’s not what you’re looking for. (That is, in what way does it not do what you’re wanting?)

If I’m not mistaken, identity is read only, and I want to take, say, the rotation of the camera (123,754,98)(this is a random number, but it gets the point through) and make it (0,0,0)

My first question would be, have you tried the code I posted? If so, did it not do what you’re wanting?

As for your post above, it doesn’t matter that Quaternion.identity is read-only, because you’re not assigning to it; rather, you’re assigning its value to Transform.rotation. Also, (123,754,98) is not a valid value for a (unit-length) quaternion anyway. First of all, quaternions have four elements, not three, and second of all, the values you specified would violate the unit-length constraint regardless of what elements they were assigned to.

Are you possibly confusing a quaternion with a set of Euler angles? And let me restate the question again: have you tried the code I posted and found it not to work?

I have tried the code, and that number I used was a random number, I did not pay attention to what typed, and I understand that 754 is not a valid number, but it was just an example. Identity did nothing. I’m not super familiar with rotation, I generally dont deal with that kind of stuff, I’m kinda clueless.

I will explain exactly what i am trying to do. I have the first person controller prefab (the newer one with the more advanced scripts like the Character motor script) and i move to a “car” (it is just a block right now, no animations for getting in or moving, it doesnt even have a script to move) and i press “i” and then I “get in” which transforms my position to right on top of it, and locks my player in place so i cant move or rotate the camera. the only problem i have is the camera and player rotation are the same as what they were just before they go on the car, and i need the player to be looking forward. here is the script I’m using

var player : Transform;
var Playercam : Transform;
var canGetIn : boolean;
var In : boolean;
var ridePoint : Transform;
var carcam : Transform;

function Awake () {
	if (player == null  GameObject.FindWithTag("Player"))
	player = GameObject.FindWithTag("Player").transform;
	
	if (carcam == null  GameObject.FindWithTag("Carcam"))
	carcam = GameObject.FindWithTag("Carcam").transform;
	
	if (ridePoint == null  GameObject.FindWithTag("carRide"))
	ridePoint = GameObject.FindWithTag("carRide").transform;
	
	if (Playercam == null  GameObject.FindWithTag("Player2"))
	Playercam = GameObject.FindWithTag("Player2").transform;
	
	carcam.gameObject.active = false;
}

function Update () {
	if (Vector3.Distance(transform.position, player.position) > 5) {
		canGetIn = false;
	}
	
	else if (Vector3.Distance(transform.position, player.position) < 5) {
		canGetIn = true;
	}
	
	if (canGetIn == true  Input.GetButtonDown("Getin")) {
		In = true;
	}
	
	else {
		In = false;
	}
	
	if (In == true) {
		player.position = ridePoint.position;
		player.GetComponent(CharacterMotor).enabled = false;
		player.GetComponent(MouseLook).sensitivityX = 0;
		Playercam.GetComponent(MouseLook).sensitivityY = 0;
		player.rotation = Quaternion.identity;
		Playercam.rotation = Quaternion.identity;
	}
}

function OnGUI () {
	if (canGetIn == true  Screen.lockCursor == true) {
		GUI.Box (Rect (Screen.width / 2 + 60, Screen.height / 2, 100, 30), "Press I to get in");
	}
}

I have an idea. What if you made an empty game object called “Windshield” attach it to the car, and whenever your character enters the car, you set your character’s rotational value to LookAt the Windshield. (You can restrain it to the Y value if you need to)

I don’t have code for this, but I just thought that maybe this crazy idea might help solve your rotation problem ^^;;

First, a couple of small points. Although it’s unlikely to have any noticeable performance impact, you’re currently ‘finding’ the various game objects in your Awake() function twice each, which is unnecessary. Instead, find them, store the result, and then operate on the result, e.g.:

if (player == null) {
    playerObject = GameObject.FindWithTag("Player");
    if (playerObject) {
        player = playerObject.transform;
    }
}

Also, this:

if (Vector3.Distance(transform.position, player.position) > 5) {
    canGetIn = false;
}
else if (Vector3.Distance(transform.position, player.position) < 5) {
    canGetIn = true;
}
	
if (canGetIn == true  Input.GetButtonDown("Getin")) {
    In = true;
}
else {
    In = false;
}

Can instead be written as (untested):

canGetIn = Vector3.Distance(transform.position, player.position) <= 5;
In = canGetIn  Input.GetButtonDown("Getin");

Technically the first conditional isn’t exactly equivalent to your code, but I’m guessing it more closely matches your intent. (Also, I don’t use UnityScript, so I can’t guarantee I got the syntax exactly right.)

As for the problem you mention, I’m still not completely sure as to what you’re trying to do, but maybe this:

player.rotation = ridePoint.rotation;
Playercam.rotation = ridePoint.rotation;

Is what you’re looking for.

that looked like it would work (by the way, I think my other code works fine right now, but i might take the provided code into consideration) but for some reason it didnt work. it didn’t change the rotation for some reason. It seemed like it would, though.