Basically. I’m trying to get the character to face the direction it is moving. I wrote the simple code below, but decided that it would be too complicated for something so simple. Is there an easier way to get a character to face the direction they are moving?
Interesting. Not quite sure how that works. Am I supposed to stick this on a child object. And grab variables from parent? Cause if I use the fpswalker with movedirection it flips around in all directions except forward.
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
transform.forward = moveDirection;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
What if I want a transition as well. I have something for moving it around… but what if I wanted to show the character rotating just a few frames so that it didn’t appear as if the character was immediately flipping around.
The other solution I’m using isn’t very good for the rotation either. As moving back forth in opposite directions causes the player to instantly flip. But any gradual movement seems to rotate nicely.
I have an object follow behind the character and the characters back points at the following object. It uses an if statement with distance to stop the following object from getting to close. It kinda works. It’s probably also wasteful though.
Isn’t it easier to just do what was originally suggested and use your current movement vector? If you move like this:
transform.Translate(0,3,0);
Then your movement vector would be (transform.right * 0 + transform.up * 3 + transform.forward * 0).normalized. If you move like this:
rigidbody.velocity += // something or other
Then your movement vector3 would be rigidbody.velocity.normalized. Either way, you can just say transform.forward = movementVector;
Yep. The tough thing about asking these questions. Is someone or everyone I guess. Has the right answer. But I have no idea what they are talking about. Not familiar with those commands. So I look it up some of it. (links below)
And the example for velocity is helpful. As for the normalized stuff. idk.
And then I take a stab at it and fail…
But I really do appreciate the help though. and the time you took to tell me about it.