- Home /
Question by
digzou · Nov 29, 2014 at 02:31 PM ·
javascriptlerprotatearoundslerp
Slerp / Lerp RotateAround()
I use this script to rotatearound 4 walls at angles of 90,180,270,360. Although this function works good, it stops abruptly. Is there a way to slow down the rotate around towards the end of the rotation. I tried using Time.timeScale but it slowed down the the whole game. Also I tried playing around with physics2d attributes like Drag,Angular Drag, rigidbody2d.AddForce() and rigidbody2d.AddTorque(). But cudnt acheive what I needed.
Just begun learning unity, so I dont know much about it.
Thanks in advance.
Here's my code:
#pragma strict
public class mgWallScript extends MonoBehaviour{
var rotZ: int=0;
var prefab : GameObject;
var tprefab : GameObject;
var thinRingPrefab : GameObject;
var nextPrefab : GameObject;
var localPrefab : GameObject;
var globalWallObj : GameObject;
var num=4;
public static var radius : float =1.56f;
var arr = new Array ();
var rotateTime = 0;
var rotateSpeed = 10;
var rotateDegrees =0;
private var rotating = false;
public var rotateing = new Array();
var counterTime:int;
static var globalObj : GameObject;
function Start(){
counterTime = 0;
rotateing[0]=false;
rotateing[1]=false;
rotateing[2]=false;
rotateing[3]= false;
rotZ=0;
for(var i=0;i<num;i++){
var angle=i*Mathf.PI*2/num;
var pos=Vector3(Mathf.Round(Mathf.Cos(angle)),Mathf.Round(Mathf.Sin(angle)),0)*radius;
var obj=Instantiate(thinRingPrefab,pos,Quaternion.Euler(0,0,rotZ));
rotZ+=90;
arr.push(obj);
}
}
function FixedUpdate(){
rotateTime = 4;
rotateDegrees=90;
if(counterTime <4){
for(var i=0;i<arr.length;i++){
tprefab=arr[i];
if(i==3)
nextPrefab=arr[0];
else
nextPrefab=arr[i+1];
RotateAllWalls(i,tprefab.transform,this.gameObject.transform, nextPrefab.transform ,-(Vector3.forward), rotateDegrees, rotateTime);
}
}else{
counterTime = 0;
rotateing[0]=false;
rotateing[1]=false;
rotateing[2]=false;
rotateing[3]= false;
}
}
function RotateAllWalls (temp:int ,thisTransform : Transform, otherTransform : Transform, nextTransform : Transform, rotateAxis : Vector3, degrees : float, totalTime : float) {
if (rotateing[temp]) return;
rotating = true;
rotateing[temp]= true;
var startRotation = thisTransform.rotation;
var startPosition = thisTransform.position;
var endRotation = nextTransform.rotation;
var endPosition = nextTransform.position;
thisTransform.rotation = startRotation;
thisTransform.position = startPosition;
var rate = degrees/totalTime;
for (var i = 0.0; i < degrees; i += Time.deltaTime * rate * rotateSpeed) {
yield;
thisTransform.RotateAround(otherTransform.position, rotateAxis, Time.deltaTime * rate * rotateSpeed);
}
thisTransform.rotation = endRotation;
thisTransform.position = endPosition;
rotating = false;
rotateing[temp]= false;
counterTime++;
}
}
Comment