Hey all. I’ve been working with 3dbuzz.com tutorials to learn unity. After I finished going through the basics and decided I would go ahead and go through with the Third Person tutorials. After getting through the implementation of the TP_Controller and TP_Motor I’ve come across a snag. The character is supposed to move towards in the direction that the camera is looking. And for the most part that works fine, the player moves side the side, forwards and backwards and turns in the direction of the camera. The only problem is that when I move forward the player moves very slowly.
I have a programming background, but 3d programming is still very new to me. I just can’t figure out whats going on with this code. lol
TP_Motor.cs
using UnityEngine;
using System.Collections;
public class TP_Motor: MonoBehaviour{
public static TP_Motor Instance;
public float MoveSpeed = 10.0f;
public Vector3 MoveVector { get; set; }
void Awake(){
Instance = this;
}
public void UpdateMotor(){
SnapAlignCharacterWithCam();
ProcessMotion();
}
void ProcessMotion(){
// Transform MoveVector to World Space
MoveVector = transform.TransformDirection( MoveVector );
// Normalize MoveVector if Magnitude > 1
if( MoveVector.magnitude > 1 )
MoveVector = Vector3.Normalize( MoveVector );
// Multiply MoveVector by MoveSpeed
MoveVector *= MoveSpeed;
// Multiply MoveVector by DeltaTime
MoveVector *= Time.deltaTime;
// Move the Actor in World Space
TP_Controller.TPController.Move( MoveVector );
}
void SnapAlignCharacterWithCam(){
if( MoveVector.x != 0 || MoveVector.z != 0 ){
transform.rotation = Quaternion.Euler( transform.eulerAngles.x, Camera.mainCamera.transform.eulerAngles.y, transform.eulerAngles.z );
}
}
}
TP_Controller.cs
using UnityEngine;
using System.Collections;
public class TP_Controller: MonoBehaviour{
public static CharacterController TPController;
public static TP_Controller Instance;
void Awake(){
TPController = GetComponent( "CharacterController" ) as CharacterController;
Instance = this;
}
// Update is called once per frame
void Update(){
if ( Camera.mainCamera == null )
return;
GetLocomotionInput();
TP_Motor.Instance.UpdateMotor();
}
void GetLocomotionInput(){
var deadZone = 0.1f;
TP_Motor.Instance.MoveVector = Vector3.zero;
if( Input.GetAxis( "Vertical" ) > deadZone || Input.GetAxis( "Vertical" ) < -deadZone ){
TP_Motor.Instance.MoveVector += new Vector3( 0, 0, Input.GetAxis( "Vertical" ) );
}
if( Input.GetAxis( "Horizontal" ) > deadZone || Input.GetAxis( "Horizontal" ) < -deadZone ){
TP_Motor.Instance.MoveVector += new Vector3( Input.GetAxis( "Horizontal" ), 0, 0 );
}
}
}