- Home /
How to rotate an object by its pivot in script
I need to rotate an object by its pivot using the mouse input, but I can't seem to find a reference in the Unity Script Reference on rotating by pivots in script. Does anyone know how to do it?
I need to be able to adjust the rotation around the pivot point using the input keys.
Answer by GoSuNeem · Nov 03, 2011 at 04:16 PM
As far as I know, I don't think you can set a pivot point.
(I am still fairly new to unity)
But What I do is make a empty game object and put childs in there... Using the parent as a pivot point and rotate that.
Hope that helps. Good luck!
This trick works well but if you are making an SD$$anonymous$$ for other programmers or artists, it's not a user-friendly approach.
Answer by BarkShark · Nov 25, 2011 at 08:43 PM
You can use pivot point in unity, next to the scale tool you see a button 'center' . Hit this button and your object will become a pivot point. I used this and it works perfectly
In editor mode.
I need it to rotate by Pivot through scripting. during gameplay
Answer by BlurMotions · Sep 02, 2016 at 09:16 AM
You can use this
private Vector3 object_pos;
private Vector3 mousePosition;
public float moveSpeed = 0.1f;
public Transform target; //Assign to the object you want to rotate
float angle;
// Use this for initialization
void Start () {
}
void Update () {
//rotation
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
In unity you can set the pivot on an object.. a 2D sprite, using the inspector when dealing with textures and with 3D You can't change the pivot atleast not directly, but you can create a custom pivot, to do this create an empty GameObject and attach to it whatever GameObject you want to this empty GameObject then from that you can set a pivot point