- Home /
Trying to rotate and move a gameobject through JavaScript, help?
Hi, I'm trying to rotate and move the player through JavaScript into a bed. I'm using the First Person Controller. There's a problem though. I have no idea how! I looked up some stuff on Answers and Google, but it was either it totally confused me or it wasn't something I was looking for. So can someone try write this for me? Explanation is GREATLY appreciated. Thanks for the help.
Answer by aldonaletto · Aug 03, 2013 at 08:05 PM
A somewhat easy way to make simple animations like this via script is to create an empty game object and place it at the desired position and rotation, then lerp to the position/rotation when needed. You should also have another empty game object defining the stand up position, so that you could lerp to it when leaving the bed.
The code would be something like this:
public var bedPos: Transform; // drag here the empty that defines the bed target position/rotation
public var bedDuration: float = 0.6; // duration in seconds
public var upPos: Transform; // drag here the empty that sets the stand up position/rotation
public var upDuration: float = 0.6;
private var moving = false;
function LerpMe(target: Transform, duration: float): IEnumerator {
var pos0 = transform.position;
var rot0 = transform.rotation;
var t: float = 0;
moving = true; // start moving now
while (t < 1){
t += Time.deltaTime/duration;
transform.position = vector3.Lerp(pos0, target.position, t);
transform.rotation = Quaternion.Slerp(rot0, target.rotation, t);
yield;
}
moving = false; // movement finished
}
You can move the player to bed with this call (player script):
if (!moving){ // do nothing if already moving
LerpMe(bedPos, bedDuration);
}
And the player can wake up like this (player script):
if (!moving){
LerpMe(upPos, upDuration);
}
NOTE: You can rotate the transform to any angle, but remember that the CharacterController itself doesn't rotate - it's always upright, aligned to the world Y. It's only the character collider that refuses to rotate, thus visually there's nothing wrong - except for the green capsule that you can see in the Scene view: it will remain up and alert while the player sleeps like a log. This may cause some problems: the player may fall trough the terrain case the bottom of the capsule penetrates the ground in the bed position, for instance. Due to this and other possible problems, it's a good idea to disable the movement script while going to bed, and enable it again when completely in the up position.
Thanks A LOT. This was very well explained. $$anonymous$$ore than I intended actually. Thank you for pointing out the possible bugs, and telling me how it's done so I can use it in the future. Also sorry for the long wait for a response. I had to go to work xD.
Your answer
Follow this Question
Related Questions
Rotate an Object 1 Answer
Link object rotation to its parent 1 Answer
Rotating an object in position 2 Answers
How do you flip a card with smooth rotation? 1 Answer
using a custom rotation in "Instantiate" 4 Answers