Rotate in Local Axis with one variable

Hello all!
I rotate my Object in World Z Axis using this:

var speedHorizontal 	: int	;

speedHorizontal = Input.GetAxis("Horizontal") *100* Time.deltaTime;

transform.Rotate(0,0,1*speedHorizontal,Space.World  );

it works.So far so good.

What I really need: I want to create variable which will control local rotations (rotation to specific angle).
Rotation should look like this: transform.Rotate(1,0,0, Space.Self);

I was trying to use code below, but it doesn’t work:

transform.localRotation.x = localAxisRotX

I have recorded video to show my problem:

var MoveHorizontal 		: int	;
var MoveVerical 		: int	;
var localAxisRotX 		: float	;

function Update () 
{

		MoveHorizontal = Input.GetAxis("Horizontal") *100* Time.deltaTime;
		MoveVerical = Input.GetAxis("Vertical") *300* Time.deltaTime;
		
	
		transform.Rotate(0,0,1*MoveHorizontal,Space.World)  ;
	//	transform.Rotate(1*MoveVerical,0,0,Space.Self);
						
		transform.localRotation.x =  localAxisRotX 	 ;	
	
}

Thank you for reading, and even more thanks for any answers or suggestions!

var RotatingSpeed = 10;
function Update ()
{
     transform.Rotate(Input.GetAxis("Horisontal") * RotatingSpeed,0,0, Space.World);
}

Maybe that?

To solve your problem try replacing the

transform.localRotation.x =  localAxisRotX   ;

with

transform.localEulerAngles.x = localAxisRotX;

And when you use the Rotate function I see that you multiply the Input.GetAxis for 1, and it is no sense because it will give you the same value XD

Hope it helps :slight_smile:

It’s not working when my object is rotating in space.world on Z Axis :frowning:

I could put my object in EmtptyGameObject and then use Transform.Rotate Z axis in space.world to rotate,
and inside on my Object use your code to rotate in local.x. , but i thing it should be better way to do this.

var MoveHorizontal 		: int	;
var localAxisRotX 		: float	;

function Update () 
{

		MoveHorizontal = Input.GetAxis("Horizontal") *100* Time.deltaTime;
	
		
		transform.Rotate(0,0,1*MoveHorizontal,Space.World);

			 
		transform.localEulerAngles.x = localAxisRotX;
	
	
}

isnt that what this line does:

transform.Rotate(0,0,1*MoveHorizontal,Space.World) ;

and should it not be this:

transform.Rotate(0,0,1*MoveHorizontal,Space.Self) ;

I need this to rotate object in Z axis in space.world.
I need additional rotation in X axis in space.local, and I want to
control this with variable which I could use to specific angle.

Just make them all mathematical and reset the starting point to zero. :wink:

var MoveHorizontal : int;
var MoveVerical   : int;
var localAxisRotX : float;
var localAxisRotY : float;
var localAxisRotZ : float;
function Update (){
	MoveHorizontal = Input.GetAxis("Horizontal") *100* Time.deltaTime;
	localAxisRotZ += MoveHorizontal;
	MoveVerical = Input.GetAxis("Vertical") *300* Time.deltaTime;
	localAxisRotX += MoveVerical;
	   
	transform.rotation = Quaternion.identity;
	transform.Rotate(0,0,1 * localAxisRotZ, Space.World);
	transform.Rotate(1*localAxisRotX,0,0,Space.Self);
}

woow its working Thank You ,
but now there is another problem. I need this object to work with rigidbody, because now when is falling on the ground, the rotation is frozen.
Is the different way to get this local rotation.x ?

please help, anyone ?

This code would work fine with a rigidbody, but this is not the way to control a rigid body.

Perhaps you could elaborate on the end result. It seems to me that you are trying to fix little problems without looking at the entire solution.

I control my Object with this : transform.Rotate(0,0,1 * localAxisRotZ, Space.World);
and I have added some forces. It looks pretty good.
But my problem is that I need add this local rotation. Physic doesn’t work on rotation with your code.
Is it any solution for that?

Again, this is “not” the way to control a rigid body.

You would use something like this:

Unity - Scripting API: Rigidbody.AddTorque to add world torque to an object and
Unity - Scripting API: Rigidbody.AddRelativeTorque to add local torque

Do this in the FixedUpdate so that it is constant enough.

this is my last request I promise :slight_smile:
Could you help me please with create
variable which I could use to specific angle.

I tried this, but…

var localAxisRotX : float;
 

function FixedUpdate () {


rigidbody.AddRelativeTorque (1*localAxisRotX, 0, 0);

}

OK, here is the conundrum that you are in… You want to specify an angle, but then have the object collide with things. This actually counteracts what you are trying to do.

That being said what you are looking for is half math, half physics. Its not easy to do, and without knowing what your trying to accomplish, just giving you code to do it will not really help you. (since giving you code hasn’t really helped you thus far.)

ok, I will try figure out something, thanks for your time :slight_smile: