1
\$\begingroup\$

I'm making a simple FPS game. I've come across this problem that I am unable to solve for quite some time. If this is question has already been asked before then do let me know. The problem at hand is that I have to use some really large values for manipulating the velocity of the player's rigid body when I make it move.

I was successfully able to make the camera and the character look around based on mouse input. After that, however, I realized I needed to change the directions of velocity that increase/decrease on input to match the character's orientation. W key has to mean player's forward no matter where he is looking. To fix that problem, I take the player's local forward vector every frame using transform.forward, add velocity/speed to its components (x and z), convert it to a global vector and apply it to the rigidbody velocity.

This fix seems to work fine with only one issue: I have to use very large values of speed to achieve a normal pace. Why is this the case? Before I implemented the FPS camera, values upto 20 - 40 would do the trick. Now, I have to use 500 - 1000 to achieve the same speed. Is this normal? Also, because of this (I think), I am experiencing (very) slight lag.

This is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterControl : MonoBehaviour
{

Header("References")]
public Rigidbody rb;
public Camera MainCamera;
public GameObject gun;
[Header("Movement")] //why is this header not coming up in the editor
float speed;
public float run = 7f;
public float sprint = 10f;
public float jump_force = 5;
public float sensH, sensV = 2f;
float verticalLook = 0;
Vector3 localForward, globalForward;

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody>();
    Cursor.lockState = CursorLockMode.Locked;
}

// Update is called once per frame
void Update()
{
    Camera();
    Movement();
}

void Movement(){
    // jump
    if (Input.GetKeyDown(KeyCode.Space)) {
        rb.AddForce(transform.up * jump_force);
    }
    // if shift -> sprint
    if (Input.GetKey(KeyCode.LeftShift)) { speed = sprint; }
    else { speed = run; }
    
    // local forward direction
    localForward = transform.forward;
    // set local speed
    localForward.x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    localForward.z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    // get global from local
    globalForward = transform.TransformVector(localForward);
    // set velocity
    rb.velocity = new Vector3(globalForward.x, rb.velocity.y, globalForward.z);

}

void Camera(){
    // apply horizontal camera view
    transform.Rotate(new Vector3(0f, Input.GetAxisRaw("Mouse X") * sensH, 0f), Space.Self);
    // handle vertical camera view
    verticalLook += Input.GetAxisRaw("Mouse Y") * -sensV;
    verticalLook = Mathf.Clamp(verticalLook, -30f, 30f);
    // apply vertical camera view
    MainCamera.transform.localRotation = Quaternion.Euler(verticalLook, 0f, 0f);
    gun.transform.localRotation = Quaternion.Euler(0f, 90f, verticalLook);
}
}

Even for jump force, I have to use values as large as 300 - 400 to get a 'good' jump.

If you take a look at the inspector at the bottom right, I have to use very high values

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.