8 direction movement. Getting look in the direction it moves.

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?

function Update () {


if(Input.GetKey("d"))
{
transform.localEulerAngles.y = 90;
}
if(Input.GetKey("a"))
{
transform.localEulerAngles.y = -90;
}

}
character.transform.forward = directionVector;

Where character is your character gameobject, and directionVector is the direction in which you are moving the character.

Hopefully that helps!

-JFFM

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;
	
	
}

I’m still having problems with this.

you can do

var move : Vector2 = Vector2.zero;

if( Input.GetKey( “d” ) ) move.x += 1f;
if( Input.GetKey( “a” ) ) move.x -= 1f;

if( Input.GetKey( “w” ) ) move.y += 1f;
if( Input.GetKey( “s” ) ) move.y -= 1f;

if( move != Vector2.zero )
transform.localEulerAngles = Vector3( 0, Mathf.Atan2(move.y, move.x) * Mathf.Rad2Deg, 0 );

You may need to +90, -90, or +180 but it should get you what you want if your model is facing the right way

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.

Well one of my sollutions is pretty bad. and doesn’t work perfectly. and is wasteful of computer resrouces imo.

var myRotation: float;


function Update () {

print (myRotation);

if(Input.GetKey("d"))
{
if(myRotation<90)
{
transform.localEulerAngles.y +=15;
myRotation += 15;
}
else if(myRotation>90)
{
transform.localEulerAngles.y -=15;
myRotation -= 15;
}
}

if(Input.GetKey("a"))
{
if(myRotation>-90)
{
transform.localEulerAngles.y -=15;
myRotation -= 15;

}

}

if(Input.GetKey("w"))
{
if(myRotation<0)
{
transform.localEulerAngles.y +=15;
myRotation += 15;
}
else if(myRotation>0)
{
transform.localEulerAngles.y -=15;
myRotation -= 15;
}
}

if(Input.GetKey("s"))
{

if(myRotation<1  myRotation>-180)
{
transform.localEulerAngles.y -=15;
myRotation -= 15;
}
else if(myRotation>0  myRotation<180)
{
transform.localEulerAngles.y +=15;
myRotation += 15;
}

}

}

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.

var movementVector = Vector3;

function Update () {

transform.Translate(0,3,0);

movementVector = (transform.right*0+transform.up*3+transform.forward*0).normalized;
rigidbody.velocity += moveVector;
movementVector = rigidbody.velocity.normalized;
//transform.forward = movementVector;

}