How to Rotate Around Axis?

Scripting Language: C#
Unity Version: 4

What I’m attempting to do is rock a platform back and forth across an axis (preferably x). Think of it as being like a platform suspended by a string, and then when you push down on one side the other lifts up but the platform itself doesn’t move, it just rocks. I attempted to handle this by changing the x axis, however that makes the platform move around as well. I finally realized what’s happening is the following things change: Position X, Position Y, Position Z, Rotation Z.

Now, the problem I’m having is replicating the proper amount of incrementing/decrementing for each of these values to keep the object rocking but not moving. I am kind of hoping someone else has tried something similar or may have more input on what concept I’m missing out on here.

If you’re not quite familiar with what I mean by “rocking,” take an item and put it in the Unity Scene, and then use the transform tool to just twist it on one axis. Here are a couple images to help illustrate as well (they’re unrelated but are the closest thing I could find to illustrating what I’m going for):

Rock Left: http://www.keison.co.uk/products/stuart/si70.jpg
Rock Right: http://www.keison.co.uk/products/stuart/si80.jpg

The end goal of this is to get a rockable platform for things like jumping games (where you just want it to “flip” in a sense, but not have movement left/right/up/down.

Any help would be much appreciated, and if there are any questions please don’t hesitate to ask. Thanks!

Take a look at Transform.RotateAround. In your case point would be the center of the platform, axis : the z axis and angle : the weigth of the object on the platform.

Thanks for the tip! I think I’m missing something though… It keeps setting the “rotate” area as what appears to be a large circle. So the platform moves as if it’s on the outside of a ball. How would I go about changing this so that the center of the platform never moves? I was trying to think of another analagous situation and a see-saw would be a perfect example (when left goes up, right goes down, but the middle never moves).

Something like this should work. This will rotate it around the z axis.

I’ve made everything public so you can check/set the values as its working.

The only thing I cant remember is if you need to pull z from localEulerAngles or eulerAngles… can try either or if one doesnt work.

public float direction = 2;
public float TargetAngle = 30;
public float CurrentZ = 0;

void Update()
{

  CurrentZ = transform.eulerAngles.z;
  if(CurrentZ  < 180.0f  CurrentZ  > TargetAngle)
    direction = -2;  
  else if(CurrentZ  > 180.0f  CurrentZ  < 360-TargetAngle)
    direction = 2;

  transform.Rotate(0,0,direction * Time.deltaTime, Space.Self);

}
1 Like

Thanks for taking the time to try and help! This is also giving similar results (though not nearly as off). Here’s an image showing the results vs. what I’d ideally like to see.

The top one shows that the center is still moving. When I move into the edit mode and rotate along just one axis it works properly (as seen in the bottom picture where the center is always in the same location).

looks like your pivot might be off.

try putting the mesh onto a new empty gameobject

I’ve attached this script to a plane mesh and I think I got what you want assuming you want the platform to rotate around it center. If not just change the first parameter to the point you want it rotate around.

	void Update () {
		
		transform.RotateAround(transform.position, Vector3.forward, Input.GetAxis("Horizontal") * Time.deltaTime);
	}

When you pressed the arrows on your keyboard you should see the platform rotating around the z axis passing through the center of the gameobject

1 Like

It appears it is… I just tried with a normal plane and it worked as expected. I also figured out that if I am see-sawing the X-axis, it will require changes to all 3 positions/rotations, and if I see-saw the Y-axis, it requires changes to all three rotations. Z is the only one that will work properly with just one change (z rotation). I’m not quite getting the theory behind why this is though (I’m new to the mathematics behind 3D spaces).

Regarding pivots, when I take, say 4 planes of equal size and line them up so they make a square (think of a tiled field), is it possible to edit the pivot point with that? Essentially what I’ve done is tile four squares together and put them into an empty object to get them “linked” to one another so the entire field moves as one. While a single tile will rotate correctly with this script, the grouping does not.

This script actually ran into the same issue I had before, but thank you for trying!


I really do appreciate this help, guys. I’ve finally made up the decision to “make something” and learn along the way, and something I felt would be easy appears to involve a little more than I had planned!

I still struggle with rotations, and Ive been messing with them for quite some time now. Gives me a brain implosion.

As for the second part of your question, the easiest way to set the pivot for something like that, is to make sure that they are all children of a parent gameobject and attach the script to that parent object.

You could do it using RotateAround, but you will have to figure out where the center is… dont ask me how to do that. haha. I rely on my pivots/rotations being set correctly from whatever modelling software im using…

This is actually how I have it:

Parent: Platform
Children: hole, solid, solid, solid (set up in a square formation)
Script: Platform (attached to parent)

When I use your script on it, it turns some along the Y-axis as well, which I think is where the issue is (in regards to it not looking right). When I use the same script on prefabs, everything works perfect.

For the meshes, I made them in Blender (just created a cube, shrank it down and saved the file in .blender format). I’m wondering if maybe that’s the issue (like there’s something special I should do when saving).

The Pictures in the given link are not visible

transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.Euler(a.x,a.y,transform.eulerAngles.z+45),Time.deltaTime);

Skype: Santhana Bharathy

Well, I just tried it, and it works perfectly for me.

  1. I created a GameObject. Reset its rotation/location to ensure its at 0,0,0 for all
  2. Created a small script called SimpleTilt.cs
using UnityEngine;
using System.Collections;

public class SimpleTilt : MonoBehaviour {

	public float MaxTilt = 30;
	
	float direction = 10;
	
	// Update is called once per frame
	void Update () {
		float CurrentZ = transform.eulerAngles.z;

	  	if(CurrentZ  < 180.0f  CurrentZ  > MaxTilt)
		    direction = -10;  
		else if(CurrentZ  > 180.0f  CurrentZ  < 360-MaxTilt)
			direction = 10;

		transform.Rotate(0,0,direction * Time.deltaTime, Space.Self);
	}
}
  1. Added various other meshes to the empty game object

rotates as you would expect, and as you are after.

Only thing I can think of is you have some other scripts moving/rotating stuff around?

1 Like

using UnityEngine;

public class Example : MonoBehaviour
{
void Update()
{
// Spin the object around the world origin at 20 degrees/second.
transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
}
}

1 Like