Question by
robotscott725 · Feb 01 at 04:17 AM ·
movementmovement scriptmovedirection
Rigidbody FPS Controller isn't Moving.
So, I was working on a rigidbody fps controller, and when I finished, my player didn't move. Here's the code, Any help is much appreciated! Code:
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
public class CharacterMovement : MonoBehaviour
{
float moveForward;
float moveSide;
float moveUp;
bool isGrounded;
public float moveSpeed = 10f;
public float sprintSpeed = 10f;
public float jumpSpeed = 10f;
float currentSpeed;
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKey(KeyCode.LeftShift) && currentSpeed != sprintSpeed)
{
currentSpeed = sprintSpeed;
}
else
currentSpeed = moveSpeed;
moveForward = Input.GetAxisRaw("Vertical") * currentSpeed;
moveSide = Input.GetAxisRaw("Horizontal") * currentSpeed;
moveUp = Input.GetAxisRaw("Jump") * jumpSpeed;
}
private void FixedUpdate()
{
rb.velocity = (transform.forward * moveForward) + (transform.right * moveSide) + (transform.up * rb.velocity.y);
if (isGrounded && moveUp != 0)
{
rb.AddForce(transform.up * moveUp, ForceMode.VelocityChange);
isGrounded = false;
Debug.Log("Jumping");
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Path Movement created from user input.mouse and player follows it. 0 Answers
How to move a cube by flipping using keyboard input 1 Answer
Movement worked yesterday but doesn't anymore? I did not make any changes in script 2 Answers
Unity 2D: player character speeds up unintantionally 3 Answers
Help changing input to axis 0 Answers