I need to rotate List i.e. spline around its center. I can easily rotate vector around its point, but how should I handle vector array? Any advice on this?
just loop through the list rotating each Vector
^This would not work. This is going rotate each Vector2, but I’m imagining what you want is to rotate EACH vector in positional space around a pivot Vector. For that, you’d need to do something a bit fancier.
There’s many ways, but I’ve whipped up a quick example which makes use of two methods: FindPointOnCircle and AngleBetweenPoints.
This wont make much sense without the enclosed scene, so best just load it up in your project.
using UnityEngine;
public class RotateGameObjectArray : MonoBehaviour {
/// <summary>
/// Editor exposed variable to act as our rotate tool.
/// </summary>
[Range(-180f,180f)]
public float rotateSpeed;
/// <summary>
/// I know you're using an array of Vector2's, but I'll use an array of
/// transforms for the sake of simplicity. The same principle will follow.
/// </summary>
Transform[] points;
/// <summary>
/// A Editor set transform of which to rotate everything around.
/// </summary>
public Transform pivotPoint;
void Awake () {
//In the scene I've placed all the objects I want to rotate under the camera, to which this script is attached.
points = new Transform[transform.childCount];
var i = 0;
foreach(Transform child in transform) {
points[i] = child;
i++;
}
}
/// <summary>
/// Here's the method that does all the magic. Save this one, because I find it's useful for many things.
/// </summary>
/// <returns>The point on circle.</returns>
/// <param name="origin">Origin point.</param>
/// <param name="radius">Radius.</param>
/// <param name="angle">Angle in degrees.</param>
Vector2 FindPointOnCircle(Vector2 origin, float radius, float angle) {
float x,y;
x = radius * Mathf.Cos (angle * Mathf.Deg2Rad) + origin.x;
y = radius * Mathf.Sin (angle * Mathf.Deg2Rad) + origin.y;
return new Vector2(x,y);
}
/// <summary>
/// Another useful method, important in this case because we dont want all the points to be on a line.
/// </summary>
/// <returns>The angle in degrees between points.</returns>
/// <param name="a">First point.</param>
/// <param name="b">Second point.</param>
float AngleBetweenPoints(Vector2 a, Vector2 b) {
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
}
void Update () {
foreach(var point in points) {
//We don't need to change the point if it's the pivot point. It's fine where it is.
if (point == pivotPoint) {
continue;
}
float angleOffset = AngleBetweenPoints(point.position, pivotPoint.position);
// I've made the necessary casts to and from Vector3, since the method should be a Vector2 method, but this example uses Vector3 points
float distanceToPivot = Vector2.Distance(point.position, pivotPoint.position);
Vector2 origin = (Vector2)pivotPoint.position;
Vector2 newPoint = (Vector2)FindPointOnCircle(origin, distanceToPivot, angleOffset - rotateSpeed * Time.deltaTime);
//Ta daa. Now if you play with the 'angle' slider in the inspector, everything will spin around the pivot transform.
point.transform.position = (Vector3)newPoint;
}
}
}