using UnityEngine;
public class MovementTesting : MonoBehaviour
{
public bool useCharacterForward = false;
public bool lockToCameraForward = false;
public float turnSpeed = 10f;
public KeyCode sprintJoystick = KeyCode.JoystickButton2;
public KeyCode sprintKeyboard = KeyCode.Space;
public bool jump = false;
private float turnSpeedMultiplier;
private float speed = 0f;
private float direction = 0f;
private bool isSprinting = false;
private Animator anim;
private Vector3 targetDirection;
private Vector2 input;
private Quaternion freeRotation;
private Camera mainCamera;
private float velocity;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
mainCamera = Camera.main;
}
// Update is called once per frame
void FixedUpdate()
{
input.x = Input.GetAxis("Horizontal");
input.y = Input.GetAxis("Vertical");
// set speed to both vertical and horizontal inputs
if (useCharacterForward)
speed = Mathf.Abs(input.x) + input.y;
else
speed = Mathf.Abs(input.x) + Mathf.Abs(input.y);
speed = Mathf.Clamp(speed, 0f, 1f);
speed = Mathf.SmoothDamp(anim.GetFloat("Speed"), speed, ref velocity, 0.1f);
anim.SetFloat("Speed", speed);
if (input.y < 0f && useCharacterForward)
direction = input.y;
else
direction = 0f;
anim.SetFloat("Direction", direction);
// set sprinting
isSprinting = ((Input.GetKey(sprintJoystick) || Input.GetKey(sprintKeyboard)) && input != Vector2.zero && direction >= 0f);
anim.SetBool("isSprinting", isSprinting);
// Update target direction relative to the camera view (or not if the Keep Direction option is checked)
UpdateTargetDirection();
if (input != Vector2.zero && targetDirection.magnitude > 0.1f)
{
Vector3 lookDirection = targetDirection.normalized;
freeRotation = Quaternion.LookRotation(lookDirection, transform.up);
var diferenceRotation = freeRotation.eulerAngles.y - transform.eulerAngles.y;
var eulerY = transform.eulerAngles.y;
if (diferenceRotation < 0 || diferenceRotation > 0) eulerY = freeRotation.eulerAngles.y;
var euler = new Vector3(0, eulerY, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(euler), turnSpeed * turnSpeedMultiplier * Time.deltaTime);
}
}
public virtual void UpdateTargetDirection()
{
if (!useCharacterForward)
{
turnSpeedMultiplier = 1f;
var forward = mainCamera.transform.TransformDirection(Vector3.forward);
forward.y = 0;
//get the right-facing direction of the referenceTransform
var right = mainCamera.transform.TransformDirection(Vector3.right);
// determine the direction the player will face based on input and the referenceTransform's right and forward directions
targetDirection = input.x * right + input.y * forward;
}
else
{
turnSpeedMultiplier = 0.2f;
var forward = transform.TransformDirection(Vector3.forward);
forward.y = 0;
//get the right-facing direction of the referenceTransform
var right = transform.TransformDirection(Vector3.right);
targetDirection = input.x * right + Mathf.Abs(input.y) * forward;
}
}
}
I tried this : but when I press the J key nothing happens.
I added this public variable :
public float jumpForce;
And this private :
private bool justJumped = false;
private Rigidbody rb;
private float distToGround;
At the Start() I added :
rb = GetComponent<Rigidbody>();
distToGround = GetComponent<Collider>().bounds.extents.y;
Than added this two functions :
private bool OnGround()
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
}
void Update()
{
if (!justJumped && Input.GetKeyDown(KeyCode.J) && OnGround())
{
justJumped = true;
}
}
And in the top of the FixedUpdate()
if (justJumped)
{
justJumped = false;
rb.AddForce(Vector2.up * jumpForce);
}
but pressing the J key does nothing.
using UnityEngine;
namespace Cinemachine.Examples
{
[AddComponentMenu("")] // Don't display in add component menu
public class CharacterMovement : MonoBehaviour
{
public bool useCharacterForward = false;
public bool lockToCameraForward = false;
public float turnSpeed = 10f;
public KeyCode sprintJoystick = KeyCode.JoystickButton2;
public KeyCode sprintKeyboard = KeyCode.Space;
public float jumpForce;
private float turnSpeedMultiplier;
private float speed = 0f;
private float direction = 0f;
private bool isSprinting = false;
private Animator anim;
private Vector3 targetDirection;
private Vector2 input;
private Quaternion freeRotation;
private Camera mainCamera;
private float velocity;
private bool justJumped = false;
private Rigidbody rb;
private float distToGround;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
mainCamera = Camera.main;
rb = GetComponent<Rigidbody>();
distToGround = GetComponent<Collider>().bounds.extents.y;
}
private bool OnGround()
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
}
void Update()
{
if (!justJumped && Input.GetKeyDown(KeyCode.J) && OnGround())
{
justJumped = true;
}
}
// Update is called once per frame
void FixedUpdate()
{
if (justJumped)
{
justJumped = false;
rb.AddForce(Vector2.up * jumpForce);
}
input.x = Input.GetAxis("Horizontal");
input.y = Input.GetAxis("Vertical");
// set speed to both vertical and horizontal inputs
if (useCharacterForward)
speed = Mathf.Abs(input.x) + input.y;
else
speed = Mathf.Abs(input.x) + Mathf.Abs(input.y);
speed = Mathf.Clamp(speed, 0f, 1f);
speed = Mathf.SmoothDamp(anim.GetFloat("Speed"), speed, ref velocity, 0.1f);
anim.SetFloat("Speed", speed);
if (input.y < 0f && useCharacterForward)
direction = input.y;
else
direction = 0f;
anim.SetFloat("Direction", direction);
// set sprinting
isSprinting = ((Input.GetKey(sprintJoystick) || Input.GetKey(sprintKeyboard)) && input != Vector2.zero && direction >= 0f);
anim.SetBool("isSprinting", isSprinting);
// Update target direction relative to the camera view (or not if the Keep Direction option is checked)
UpdateTargetDirection();
if (input != Vector2.zero && targetDirection.magnitude > 0.1f)
{
Vector3 lookDirection = targetDirection.normalized;
freeRotation = Quaternion.LookRotation(lookDirection, transform.up);
var diferenceRotation = freeRotation.eulerAngles.y - transform.eulerAngles.y;
var eulerY = transform.eulerAngles.y;
if (diferenceRotation < 0 || diferenceRotation > 0) eulerY = freeRotation.eulerAngles.y;
var euler = new Vector3(0, eulerY, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(euler), turnSpeed * turnSpeedMultiplier * Time.deltaTime);
}
}
public virtual void UpdateTargetDirection()
{
if (!useCharacterForward)
{
turnSpeedMultiplier = 1f;
var forward = mainCamera.transform.TransformDirection(Vector3.forward);
forward.y = 0;
//get the right-facing direction of the referenceTransform
var right = mainCamera.transform.TransformDirection(Vector3.right);
// determine the direction the player will face based on input and the referenceTransform's right and forward directions
targetDirection = input.x * right + input.y * forward;
}
else
{
turnSpeedMultiplier = 0.2f;
var forward = transform.TransformDirection(Vector3.forward);
forward.y = 0;
//get the right-facing direction of the referenceTransform
var right = transform.TransformDirection(Vector3.right);
targetDirection = input.x * right + Mathf.Abs(input.y) * forward;
}
}
}
}
If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):
https://discussions.unity.com/t/855344
That one has run, walk, jump, slide, crouch… it’s crazy-nutty!!
If you want to debug what you have,
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
- the code you think is executing is not actually executing at all
- the code is executing far EARLIER or LATER than you think
- the code is executing far LESS OFTEN than you think
- the code is executing far MORE OFTEN than you think
- the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
- is this code even running? which parts are running? how often does it run? what order does it run in?
- what are the values of the variables involved? Are they initialized? Are the values reasonable?
- are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: