Everything was going fine for me up until the section where we replaced the move variable with a script in the “TP_Motor Move Speed Theory and Implementation” video.
I followed it meticulously and watched the video several times, I worked out little typos and what not, and when I run the script I get no errors, yet my character capsule will not move. I can jump, and when I jump I can choose a direction, but the capsule itself will not move and I am losing my mind trying to find the error.
In the video he states that the character will not move unless you add the “TP_Animator.Instance.DetermineCurrentMoveDirection();” to the void GetLocomotionInput() section of TP_Controller, and yes it is added, but nope, my character fails to move unless I jump.
I’m not sure whats happening with your switch statement, it won’t compile for me because it’s wrong:
float MoveSpeed()
{
var moveSpeed = 0f;
switch (TP_Animator.Instance.MoveDirection)
{
case TP_Animator.Direction.Stationary:
// Whats going on here with the 'switch'?
moveSpeed = 0;switch (TP_Animator.Instance.MoveDirection) /
{
My working TP_Motor:
using UnityEngine;
using System.Collections;
public class TP_Motor : MonoBehaviour
{
public static TP_Motor Instance;
public float ForwardSpeed = 10f;
public float BackwardSpeed = 4f;
public float StrafingSpeed = 7f;
public float SlideSpeed = 10f;
public float JumpSpeed = 6f;
public float Gravity = 21f;
public float TerminalVelocity = 20f;
public float SlideThreshold = 0.6f;
public float MaxControllableSlideMagnitude = 0.4f;
private Vector3 slideDirection;
public Vector3 MoveVector { get; set; }
public float VerticalVelocity { get; set; }
void Awake ()
{
Instance = this;
}
public void UpdateMotor ()
{
SnapAlignCharacterWithCamera();
ProcessMotion();
}
void ProcessMotion()
{
// Transform MoveVector to World Space
MoveVector = transform.TransformDirection(MoveVector);
// Normalise MoveVector if Magnitude > 1
if (MoveVector.magnitude > 1)
MoveVector = Vector3.Normalize(MoveVector);
// Apply sliding if applicable
ApplySlide();
// Multiply MoveVector by MoveSpeed
MoveVector *= MoveSpeed();
// Apply Vertical Velocity to MoveVector.y
MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z);
// Apply gravity
ApplyGravity();
// Move the Character in World Space
TP_Controller.CharacterController.Move(MoveVector * Time.deltaTime);
}
void ApplyGravity()
{
if (MoveVector.y > -TerminalVelocity)
{
MoveVector = new Vector3(MoveVector.x, MoveVector.y - Gravity * Time.deltaTime, MoveVector.z);
}
if (TP_Controller.CharacterController.isGrounded MoveVector.y < -1)
{
MoveVector = new Vector3(MoveVector.x, -1, MoveVector.z);
}
}
void ApplySlide()
{
if (!TP_Controller.CharacterController.isGrounded)
return;
slideDirection = Vector3.zero;
RaycastHit hitInfo;
if (Physics.Raycast(transform.position, Vector3.down, out hitInfo))
{
if (hitInfo.normal.y < SlideThreshold)
{
slideDirection = new Vector3(hitInfo.normal.x, -hitInfo.normal.y, hitInfo.normal.z);
}
if (slideDirection.magnitude < MaxControllableSlideMagnitude)
{
MoveVector += slideDirection;
}
else
{
MoveVector = slideDirection;
}
}
}
public void Jump()
{
if (TP_Controller.CharacterController.isGrounded)
{
VerticalVelocity = JumpSpeed;
}
}
void SnapAlignCharacterWithCamera()
{
if(MoveVector.x != 0 || MoveVector.z != 0)
{
transform.rotation = Quaternion.Euler(transform.eulerAngles.x,
Camera.mainCamera.transform.eulerAngles.y,
transform.eulerAngles.z);
}
}
float MoveSpeed()
{
var moveSpeed = 0f;
switch(TP_Animator.Instance.MoveDirection)
{
case TP_Animator.Direction.Stationary:
moveSpeed = 0f;
break;
case TP_Animator.Direction.Forward:
moveSpeed = ForwardSpeed;
break;
case TP_Animator.Direction.Backward:
moveSpeed = BackwardSpeed;
break;
case TP_Animator.Direction.Left:
moveSpeed = StrafingSpeed;
break;
case TP_Animator.Direction.Right:
moveSpeed = StrafingSpeed;
break;
case TP_Animator.Direction.LeftForward:
moveSpeed = ForwardSpeed;
break;
case TP_Animator.Direction.RightForward:
moveSpeed = ForwardSpeed;
break;
case TP_Animator.Direction.LeftBackward:
moveSpeed = BackwardSpeed;
break;
case TP_Animator.Direction.RightBackward:
moveSpeed = BackwardSpeed;
break;
}
if (slideDirection.magnitude > 0)
moveSpeed = SlideSpeed;
return moveSpeed;
}
}
If cutting and pasting that doesn’t work, it could also be a problem in TP_Controller - or even with your character itself. It’s a bit of a pain but if you could zip and upload your project I could have a quick look and see if i can figure out the problem!
Ah… I only suggested that because I thought I recalled something about it in the videos, and that’s how I have mine. (Mine’s really messed up now, because I’ve been messing around with trying to get it to work with mecanim)
Looking through your code again, yea, your switch is messed up like Kiada said. For some reason you have 2 stationary, and for some other reason that you open up a new curly braces in the middle of it. I didn’t think much of it because I skimmed through it and you said it compiled. It should be missing a closing curly braces to complete the class.
Edit: Ah I see what happened there… you seemed to have copy and pasted the switch opening statement with it’s first case.