Whats wrong with this code? (501648)

hi guys im looking for 3rd person controller cuz my 3rd person controller is not working when i try to build run my project,
my problem with this code is,i dont know how to set this controller to face or rotate in corresponding direction,i mean face the the model in its direction,thats the only problem with this code so can you pls look at this so you can help me :smile: thx guys,srry for my english,

// ThirdPersonWalker.js

// this script adds two things to the default FPS walker
// - It adds movement forward by holding down both mouse keys
// - and it adds auto animation changing between idle, walk and run
// After adding this script to your controller, drag the character model to Target

var speed = 6.0; // standard running speed
var walkSpeed = .8; // walk speed adjust both for your charcter so feet don't slide on ground
var jumpSpeed = 8.0;
var gravity = 20.0;
var Character : Transform;

private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
// a flag to determine idle vs walking/running (so animations do not get started over and over)
private var walking : boolean = false;
// a flag to init idle animation at beginning
private var startup : boolean = true;
// a flag to determine walking vs running
private var running : boolean = true;

function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

// if both mouse buttons are down, move forward
if (Input.GetMouseButton(0)  Input.GetMouseButton(1)) 
{ 
moveDirection.z = 1;
}

moveDirection = transform.TransformDirection(moveDirection);
if(running == true){
moveDirection *= speed;
} else {
moveDirection *= walkSpeed;
}

if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}

// auto toggle between idle and walking animations - based on run / walk switch
if(Character){
// toggle between walk and run with <left shift> R
if(Input.GetKey(KeyCode.LeftShift)  Input.GetKeyDown(KeyCode.R)){
if(running == true) {
running = false;
if(walking == true) Character.animation.CrossFade("walk");
} else {
running = true;
if(walking == true) Character.animation.CrossFade("run");
}
}
if(startup == true){
startup = false;
Character.animation.Play("idle");
}
if((moveDirection == Vector3.zero)(walking == true)){
walking = false;
Character.animation.CrossFade("idle");
} else {
if((moveDirection != Vector3.zero)(walking == false)){
walking = true;
if(running == true){
Character.animation.CrossFade("run");
} else {
Character.animation.CrossFade("walk");
}
}
}
}
}

// 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;
}
@script RequireComponent(CharacterController)

Are you looking for transform.Rotate()?

yes,but i dont know how to put it in,when i ever i put code like this

its error and this code

this got error to,so how to put it?

Looks like moveDirection is the direction of movement. You always want the player to be facing towards this direction?

If so, then you might want to try:

transform.rotation = Quaternion.Euler(moveDirection);

Changing transform.rotation allows you to explicitly set the rotation of an object relative to the world (use localRotation to set it relative to its parent, if that’s what you need instead).
Since rotations are handled as quaternions, you need to use Quaternion.Euler() on your movement vector to change it into a quaternion.

hmm sir its not working, i put it and my object still not facing its direction,is there anyproblem with those code i post?

Oh, that would be because I completely spaced out on that one. Sorry, try this instead…

transform.forward = moveDirection.normalized;

aw,this works but my model looks creepy ahahahaha,when i use left and right key it rotate randomly and facing everywhere :frowning: