LookAt is called from a script directly in the character, the movement it self so walking forward is just basic unity thing that you cna put in there. And there is the script that determines were the character looks at. The part that is red is were i use the transform.LookAt.
var cube : Rigidbody;
var hit : RaycastHit;
var muis : GameObject;
var mpos360 : Vector2;
//private var layerMask : int = 1 << 9;
var dir : Vector3;
var speed : float = 10.0;
var point : Vector3;
var aimCasting=false;
var aoeCasting=false;
var aimArrow : Transform;
var spawned=false;
var aoe : Transform;
var casting=false;
var ray;
var aimProjector;
// The speed when walking
var walkSpeed = 3.0;
// after trotAfterSeconds of walking we trot with trotSpeed
var trotSpeed = 4.0;
// when pressing “Fire3” button (cmd) we start running
var runSpeed = 10.0;
var inAirControlAcceleration = 3.0;
// How high do we jump when pressing jump and letting go immediately
var jumpHeight = 2;
// We add extraJumpHeight meters on top when holding the button down longer while jumping
var extraJumpHeight = 2.5;
// The gravity for the character
var gravity = 20.0;
// The gravity in cape fly mode
var capeFlyGravity = 2.0;
var speedSmoothing = 10.0;
var rotateSpeed = 500.0;
var trotAfterSeconds = 3.0;
var canJump = true;
var canCapeFly = true;
var canWallJump = false;
var savefor : Vector3;
var waitsnap : boolean;
var savevar : float;
var startsign : boolean;
private var jumpRepeatTime = 0.05;
private var wallJumpTimeout = 0.15;
private var jumpTimeout = 0.15;
private var groundedTimeout = 0.25;
// The camera doesnt start following the target immediately but waits for a split second to avoid too much waving around.
private var lockCameraTimer = 0.0;
// The current move direction in x-z
private var moveDirection = Vector3.zero;
// The current vertical speed
private var verticalSpeed = 0.0;
// The current x-z move speed
private var moveSpeed = 0.0;
// The last collision flags returned from controller.Move
private var collisionFlags : CollisionFlags;
// Are we jumping? (Initiated with jump button and not grounded yet)
private var jumping = false;
private var jumpingReachedApex = false;
// Are we moving backwards (This locks the camera to not do a 180 degree spin)
private var movingBack = false;
// Is the user pressing any keys?
public var isMoving = false;
// When did the user start walking (Used for going into trot after a while)
private var walkTimeStart = 0.0;
// Last time the jump button was clicked down
private var lastJumpButtonTime = -10.0;
// Last time we performed a jump
private var lastJumpTime = -1.0;
// Average normal of the last touched geometry
private var wallJumpContactNormal : Vector3;
private var wallJumpContactNormalHeight : float;
// the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
private var lastJumpStartHeight = 0.0;
// When did we touch the wall the first time during this jump (Used for wall jumping)
private var touchWallJumpTime = -1.0;
private var inAirVelocity = Vector3.zero;
private var lastGroundedTime = 0.0;
private var lean = 0.0;
// The vertical/horizontal input axes and jump button from user input, synchronized over network
public var verticalInput : float;
public var horizontalInput : float;
public var jumpButton : boolean;
public var getUserInput : boolean = true;
function Start () {
if (GameObject.Find(“Third Person Player(Clone)Remote”) == 0)
{
transform.position = Vector3(15,3,15);
}
else
{
transform.position = Vector3(45,3,45);
}
}
print(GameObject.Find(“Third Person Player(Clone)Remote”));
function Awake ()
{
//moveDirection = transform.TransformDirection(Vector3.forward);
}
function UpdateSmoothedMovementDirection ()
{
var cameraTransform = Camera.main.transform;
var grounded = IsGrounded();
// Forward vector relative to the camera along the x-z plane
if (!Input.GetMouseButton(0) waitsnap == false)
{
savefor = cameraTransform.TransformDirection(Vector3.forward);
}
if (Input.GetMouseButtonUp(0))
{
startsign = true;
}
if (startsign == true)
{
savevar = savevar + 1;
}
if (Input.GetMouseButton(0))
{
waitsnap = true;
}
if (savevar == 100)
{
waitsnap = false;
savevar = 0;
startsign = false;
}
var forward = savefor;
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
if (getUserInput) {
verticalInput = Input.GetAxisRaw(“Vertical”);
horizontalInput = Input.GetAxisRaw(“Horizontal”);
}
// Are we moving backwards or looking backwards
if (verticalInput < -0.2)
movingBack = true;
else
movingBack = false;
var wasMoving = isMoving;
isMoving = Mathf.Abs (horizontalInput) > 0.1 || Mathf.Abs (verticalInput) > 0.1;
// Target direction relative to the camera
var targetDirection = horizontalInput * right + verticalInput * forward;
// Grounded controls
if (grounded)
{
// Lock camera for short period when transitioning moving standing still
lockCameraTimer += Time.deltaTime;
if (isMoving != wasMoving)
lockCameraTimer = 0.0;
// We store speed and direction seperately,
// so that when the character stands still we still have a valid forward direction
// moveDirection is always normalized, and we only update it if there is user input.
if (targetDirection != Vector3.zero)
{
// If we are really slow, just snap to the target direction
if (moveSpeed < walkSpeed * 0.9 grounded)
{
moveDirection = targetDirection.normalized;
}
// Otherwise smoothly turn towards it
else
{
moveDirection = Vector3.RotateTowards(moveDirection, targetDirection, rotateSpeed * Mathf.Deg2Rad * Time.deltaTime, 1000);
moveDirection = moveDirection.normalized;
}
}
// Smooth the speed based on the current target direction
var curSmooth = speedSmoothing * Time.deltaTime;
// Choose target speed
//* We want to support analog input but make sure you cant walk faster diagonally than just forward or sideways
var targetSpeed = Mathf.Min(targetDirection.magnitude, 1.0);
// Pick speed modifier
if (Input.GetButton (“Fire3”))
{
targetSpeed *= runSpeed;
}
else if (Time.time - trotAfterSeconds > walkTimeStart)
{
targetSpeed *= trotSpeed;
}
else
{
targetSpeed *= walkSpeed;
}
moveSpeed = Mathf.Lerp(moveSpeed, targetSpeed, curSmooth);
// Reset walk time start when we slow down
if (moveSpeed < walkSpeed * 0.3)
walkTimeStart = Time.time;
}
// In air controls
else
{
// Lock camera while in air
if (jumping)
lockCameraTimer = 0.0;
if (isMoving)
inAirVelocity += targetDirection.normalized * Time.deltaTime * inAirControlAcceleration;
}
}
function ApplyWallJump ()
{
// We must actually jump against a wall for this to work
if (!jumping)
return;
// Store when we first touched a wall during this jump
if (collisionFlags == CollisionFlags.CollidedSides touchWallJumpTime < 0)
{
touchWallJumpTime = Time.time;
}
// The user can trigger a wall jump by hitting the button shortly before or shortly after hitting the wall the first time.
var mayJump = lastJumpButtonTime > touchWallJumpTime - wallJumpTimeout lastJumpButtonTime < touchWallJumpTime + wallJumpTimeout;
if (!mayJump)
return;
// Prevent jumping too fast after each other
if (lastJumpTime + jumpRepeatTime > Time.time)
return;
wallJumpContactNormal.y = 0;
if (wallJumpContactNormal != Vector3.zero)
{
moveDirection = wallJumpContactNormal.normalized;
// Wall jump gives us at least trotspeed
moveSpeed = Mathf.Clamp(moveSpeed * 1.5, trotSpeed, runSpeed);
}
else
{
moveSpeed = 0;
}
verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight);
DidJump();
SendMessage(“DidWallJump”, null, SendMessageOptions.DontRequireReceiver);
}
function ApplyJumping ()
{
// Prevent jumping too fast after each other
if (lastJumpTime + jumpRepeatTime > Time.time)
return;
if (IsGrounded()) {
// Jump
// - Only when pressing the button down
// - With a timeout so you can press the button slightly before landing
if (canJump Time.time < lastJumpButtonTime + jumpTimeout) {
verticalSpeed = CalculateJumpVerticalSpeed (jumpHeight);
DidJump();
}
}
}
function ApplyGravity ()
{
// Apply gravity
if (getUserInput)
jumpButton = Input.GetButton(“Jump”);
// * When falling down we use capeFlyGravity (only when holding down jump)
// var capeFly = canCapeFly verticalSpeed <= 0.0 jumpButton jumping;
// When we reach the apex of the jump we send out a message
if (jumping !jumpingReachedApex verticalSpeed <= 0.0)
{
jumpingReachedApex = true;
SendMessage(“DidJumpReachApex”, SendMessageOptions.DontRequireReceiver);
}
// * When jumping up we don’t apply gravity for some time when the user is holding the jump button
// This gives more control over jump height by pressing the button longer
// var extraPowerJump = IsJumping () verticalSpeed > 0.0 jumpButton transform.position.y < lastJumpStartHeight + extraJumpHeight;
// if (capeFly)
// verticalSpeed -= capeFlyGravity * Time.deltaTime;
// else if (extraPowerJump)
// return;
if (IsGrounded ())
verticalSpeed = -gravity * 0.2;
else
verticalSpeed -= gravity * Time.deltaTime;
}
function CalculateJumpVerticalSpeed (targetJumpHeight : float)
{
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * targetJumpHeight * gravity);
}
function DidJump ()
{
jumping = true;
jumpingReachedApex = false;
lastJumpTime = Time.time;
lastJumpStartHeight = transform.position.y;
touchWallJumpTime = -1;
lastJumpButtonTime = -10;
}
function Update() {
muis = GameObject.Find(“mouse”);
mpos360.x = muis.transform.position.xScreen.width;
mpos360.y = muis.transform.position.yScreen.height;
if (aoeCasting){
Debug.Log(“AOE”);
ray = Camera.main.ScreenPointToRay(mpos360);
Physics.Raycast(Camera.main.transform.position,ray.direction,hit, 1000);
//Debug.DrawLine (ray.origin, hit.point, Color.green);
if (!spawned) {
Instantiate(aoe, Vector3(hit.point.x,hit.point.y+5,hit.point.z), Quaternion.Euler(90, hit.point.y, hit.point.z));
spawned=true;
}
if (spawned){
GameObject.Find(“AOE(Clone)”).transform.position = Vector3(hit.point.x,hit.point.y+5,hit.point.z);
}
if (Input.GetMouseButtonDown (0)) {
Destroy (GameObject.Find(“AOE(Clone)”));
aoeCasting=false;
spawned=false;
var clone : Rigidbody = Instantiate(cube, hit.point,Quaternion.identity);
casting=false;
}
}
if (aimCasting){
Debug.Log(“Aim”);
ray = Camera.main.ScreenPointToRay(mpos360);
Physics.Raycast(Camera.main.transform.position,ray.direction,hit, 1000);
aimProjector = GameObject.Find(“spawnPoint”).transform;
//Debug.DrawLine (ray.origin, hit.point, Color.green);
if (!spawned) {
Instantiate(aimArrow,Vector3(transform.position.x,6,transform.position.z), Quaternion.Euler(90, 0, 0));
spawned=true;
}
if (spawned){
GameObject.Find(“AimArrow(Clone)”).transform.position = Vector3(transform.position.x,6.5,transform.position.z);
dir = hit.point - transform.position;
//GameObject.Find(“AimArrow(Clone)”).transform.rotation = Vector3(90,dir,0);
}
if (Input.GetMouseButtonDown (0)) {
Destroy (GameObject.Find(“AimArrow(Clone)”));
aimCasting=false;
spawned=false;
var fireBall : Rigidbody = Instantiate(cube, GameObject.Find(“spawnPoint”).transform.position,Quaternion.identity);
fireBall.velocity = transform.TransformDirection(Vector3.forward*10);
casting=false;
}
}
if (!casting Input.GetButtonDown (“1”)){
aimCasting=true;
casting=true;
}
if (!casting Input.GetButtonDown (“2”)){
aoeCasting=true;
casting=true;
}
if (casting !Input.GetKey (“up”))
{
transform.LookAt(hit.point);
}
if (!IsGrounded())
{
canJump = false;
}
if (IsGrounded())
{
canJump = true;
}
if (getUserInput) {
if (Input.GetButtonDown (“Jump”))
lastJumpButtonTime = Time.time;
}
else {
if (jumpButton)
lastJumpButtonTime = Time.time;
}
UpdateSmoothedMovementDirection();
// Apply gravity
// - extra power jump modifies gravity
// - capeFly mode modifies gravity
ApplyGravity ();
// Perform a wall jump logic
// - Make sure we are jumping against wall etc.
// - Then apply jump in the right direction)
if (canWallJump)
ApplyWallJump();
// Apply jumping logic
ApplyJumping ();
// Calculate actual motion
var movement = moveDirection * moveSpeed + Vector3 (0, verticalSpeed, 0) + inAirVelocity;
movement *= Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
wallJumpContactNormal = Vector3.zero;
collisionFlags = controller.Move(movement);
// Set rotation to the move direction
if (IsGrounded() moveDirection != Vector3.zero)
{
transform.rotation = Quaternion.LookRotation(moveDirection);
}
else
{
var xzMove = movement;
xzMove.y = 0;
if (xzMove.magnitude > 0.001)
{
transform.rotation = Quaternion.LookRotation(xzMove);
}
}
// We are in jump mode but just became grounded
if (IsGrounded())
{
lastGroundedTime = Time.time;
inAirVelocity = Vector3.zero;
if (jumping)
{
jumping = false;
SendMessage(“DidLand”, SendMessageOptions.DontRequireReceiver);
}
}
}
function OnControllerColliderHit (hit : ControllerColliderHit )
{
// Debug.DrawRay(hit.point, hit.normal);
if (hit.moveDirection.y > 0.01)
return;
wallJumpContactNormal = hit.normal;
}
function GetSpeed () {
return moveSpeed;
}
function IsJumping () {
return jumping;
}
function IsGrounded () {
return (collisionFlags CollisionFlags.CollidedBelow) != 0;
}
function SuperJump (height : float)
{
verticalSpeed = CalculateJumpVerticalSpeed (height);
collisionFlags = CollisionFlags.None;
DidJump();
}
function SuperJump (height : float, jumpVelocity : Vector3)
{
verticalSpeed = CalculateJumpVerticalSpeed (height);
inAirVelocity = jumpVelocity;
collisionFlags = CollisionFlags.None;
DidJump();
}
function GetDirection () {
return moveDirection;
}
function IsMovingBackwards () {
return movingBack;
}
function GetLockCameraTimer ()
{
return lockCameraTimer;
}
function GetLean () : float
{
return 0.0;
}
function HasJumpReachedApex ()
{
return jumpingReachedApex;
}
function IsGroundedWithTimeout ()
{
return lastGroundedTime + groundedTimeout > Time.time;
}
function IsCapeFlying ()
{
// * When falling down we use capeFlyGravity (only when holding down jump)
if (getUserInput)
jumpButton = Input.GetButton(“Jump”);
return canCapeFly verticalSpeed <= 0.0 jumpButton jumping;
}
function Reset ()
{
gameObject.tag = “Player”;
}
// Require a character controller to be attached to the same game object
@script RequireComponent(CharacterController)
@script AddComponentMenu(“Third Person Player/Third Person Controller”)