0
\$\begingroup\$

As per my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using Cursor = UnityEngine.Cursor;

public class FirstPersonController : MonoBehaviour
{
    public float speed = 5;
    public float jumpPower = 4;

    Rigidbody rb;
    CapsuleCollider col;


    private GameObject player;
    private float minClamp = -45;
    private float maxClamp = 45;
    [HideInInspector]
    public Vector2 rotation;
    private Vector2 currentLookRot;
    private Vector2 rotationV = new Vector2(0,0);
    public float lookSensitivity = 2;
    public float lookSmoothDamp = 0.1f;
    public Camera cam;
    public GameObject crossHair;
    bool isActive;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.visible = false;
        Cursor.lockState = CursorLockMode.Locked;

        rb = GetComponent<Rigidbody>();
        col = GetComponent<CapsuleCollider>();
        player = transform.gameObject;
        crossHair = GameObject.FindWithTag("CrossHair");


    }




    // Update is called once per frame
        void Update()
    {
        float Horizontal = Input.GetAxis("Horizontal") * speed;
        float Vertical = Input.GetAxis("Vertical") * speed;
        Horizontal *= Time.fixedDeltaTime;
        Vertical *= Time.fixedDeltaTime;
        transform.Translate(Horizontal, 0, Vertical);




        if (isGrounded() && Input.GetButtonDown("Jump"))

        {
            rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
        }

        if (Input.GetKeyDown("escape"))
        {
            Cursor.lockState = CursorLockMode.None;
        }

        if (Input.GetButtonDown("Sprint"))
        {
            speed = 15;
            //Debug.Log("Sprint Button Held Down");
        }

        if (Input.GetButtonUp("Sprint"))
        {
            speed = 5;
            //Debug.Log("Sprint Button Let Go");
        }

        if (Input.GetKeyDown(KeyCode.H))
        {
            isActive = !isActive;
        }

        if (isActive)
        {
            crossHair.SetActive(true);
        }
        else
        {
            crossHair.SetActive(false);
        }



        rotation.y += Input.GetAxis("Mouse Y") * lookSensitivity;
        rotation.y = Mathf.Clamp(rotation.y, minClamp, maxClamp);
        player.transform.RotateAround(transform.position, Vector3.up, Input.GetAxis("Mouse X") * lookSensitivity);
        currentLookRot.y = Mathf.SmoothDamp(currentLookRot.y, rotation.y, ref rotationV.y, lookSmoothDamp);
        cam.transform.localEulerAngles = new Vector3(-currentLookRot.y, 0, 0);
    }

    private bool isGrounded()
    {
        return Physics.Raycast(transform.position, Vector3.down, col.bounds.extents.y + 0.1f);
    }



}

I can't figure out what I am doing wrong in my code, I tried to write a first person controller from scratch.

The full project can be downloaded from: https://github.com/Some-T/FirstPersonController-CSharp

My player goes through any wall or object easily despite it having colliders on the objects and a rigidbody and collider on the player.

I have gone through my code line by line and I cannot work out what is wrong? If someone could please advise me what to change or add to my code to fix this, or / and explain the concept, presumably physics related, as to why this is going wrong please?

\$\endgroup\$
4
  • \$\begingroup\$ Remember that you're not the only developer to ever try moving something with transform.Translate. Search for your keywords to find existing answers to your questions. \$\endgroup\$
    – DMGregory
    Jun 10, 2020 at 22:50
  • \$\begingroup\$ Could you elaborate on that please? Is transform.translate incorrect? \$\endgroup\$
    – SomeT
    Jun 10, 2020 at 22:52
  • \$\begingroup\$ For your context, yes, unless you did a physics query first to verify there are no walls in the way before moving there. Click the link for a full explanation. \$\endgroup\$
    – DMGregory
    Jun 10, 2020 at 22:52
  • \$\begingroup\$ Thanks for the help. \$\endgroup\$
    – SomeT
    Jun 11, 2020 at 1:31

0