Hi, I need some rotation function that rotates the entire level, but the rotation axis should be the player’s transform… I tried different ways, but nothing’s really working, because I need something like Lerp(), not the normal Rotate() functions.
Anyone got an idea?
ps: I need it for this code:
var mainCam : Camera;
var player : GravityWalker1;
var pos : Vector3;
var rot = 90.0;
private var actualRot : Vector3;
private var normBase : Vector3;
var norm : Vector3;
var gravity : Vector3;
var world : LayerMask;
var level : Transform;
var t = 0.0;
var rotSpeed = 0.01;
var turned : boolean = false;
var lerp : boolean = true;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var ray : RaycastHit;
if (Physics.Raycast(transform.position, transform.forward, ray, 100.0, world)) {
Physics.gravity = Vector3.zero;
actualRot = level.transform.eulerAngles;
t = 0.0;
turned = true;
pos = ray.point;
normBase = Vector3.Normalize(ray.normal)*rot;
norm.x = -normBase.z;
norm.z = normBase.x;
norm.y = -normBase.y;
}
}
if (turned == true) {
if (t < 1.0) {
t += rotSpeed;
}
if (lerp) {
level.eulerAngles = Vector3.Lerp(actualRot, norm, t);
}
else {
level.RotateAround(level.position, transform.position, rot * Time.deltaTime);
}
}
}
function Rotated () {
turned = true;
yield;
}
It looks like you’re on the right track - you’re using “RotateAround”. But I think you’re feeding it the wrong values.
The parameters for RotateAround are these:
theTransform.RotateAround(point, axis, angle);
‘point’ is a Vector3 and represents the position in space to act as the centre of rotation. To rotate your level around the player, you should provide the player’s position here.
‘axis’ is also a Vector3, but represents the direction of the axis to rotate around. I’m not sure how your level is laid out, but you probably want your player’s ‘up’ axis.
So, your final code to rotate the level around the player might look something like this:
var playerPos = player.gameObject.transform.position;
var playerUp = player.gameObject.transform.up;
level.RotateAround(playerPos, playerUp, rot * Time.deltaTime);
In fact, I’m only using the .Lerp() function in the code, the RotateAround() isn’t working like I want… I need something that lets me rotate the level around the player from it’s current rotation to another predefined rotation in a short time (±1 sec.)
So, if I was using rotateAround(), I had to calculate the correct angle, and stop the rotation when it arrived at the right rotation. That’d be too ifficult, so I tried to use lerp()…
You still have to control the starting and stopping if you’re using Lerp. Lerp doesn’t trigger some action which continues after you called it. It just gives you back a value at the moment that you use it. It doesn’t initiate animation. This seems to be a common misunderstanding.
Lerp is just a function which conveniently translates a range from 0 to 1 to some other specified range.
var t = 0.0;
var rotSpeed = 0.01;
var norm : Vector3;
private var actualRot : Vector3;
function Update () {
if (t < 1.0) {
t += rotSpeed;
}
level.eulerAngles = Vector3.Lerp(actualRot, norm, t);
}