0
\$\begingroup\$

I've been having this problem in the last couple of hours and I was just thinking if someone could help me figure it out, since I am unfortunately not a programmer, or at least a good one at all.

I'm creating a game heavily based off on the classic Resident Evil games, with fixed cameras and tank controls. So far so good, I created a Player Movement script that allows me to, well, control the player and got it to walk, run, go backwards without running, pretty much your standard Resident Evil classic. Now onto the scene itself, I am having problems with my character not colliding with the background. I set up a couple of cubes to test the cameras and got to reutilize those cubes as a way to use them as "walls". What happens is that I set a Raycast, it can indeed detect the collision, but that's all I got to. I decided to try and set speed to 0 or anything like that, but he just got stuck in place and couldn't move. I realized maybe the if functions are just working once, meaning that the code goes through everything until it reaches the speed = 0 point of the Raycast, in which it stops working and I can only rotate the character.

My train of thought is that once the player hits the collider of the cube, has to set the speed to 0 and while rotating, if the player is still holding w or w+shift, it will slowly regain speed until it regains full speed again, both walking or running. Same thing when walking backwards. (by the way, I'm not using RigidBody as I believe they didn't use anything like that back then, and also I did try it but it still got me through the cubes)

My variables are

public CharacterController controller;
public float speed = 2f;
public float rotateSpeed = 150f;
public float run = 3f;
public float prange = 0.5f;
public float nrange = -0.5f;
public bool movement;
public bool faster;

Here is my code in case someone can help out! Thank you very much!

public class Player_Movement : MonoBehaviour


void Update()
{
    float horizontal = Input.GetAxisRaw("Horizontal");
    Vector3 rotate = new Vector3(0f, horizontal, 0f).normalized;
    float vertical = Input.GetAxisRaw("Vertical");
    Vector3 direction = new Vector3(0f, 0f, vertical).normalized;

    if (direction.magnitude >= 0.1f || rotate.magnitude >= 0.1f)
    {
        movement = true;
        controller.transform.Translate(direction * speed * Time.deltaTime);
        controller.transform.Rotate(rotate * rotateSpeed * Time.deltaTime);

        if (Input.GetKey("w"))
        {
            float running = Input.GetAxisRaw("Run");
            Vector3 runSpeed = new Vector3(0f, 0f, running).normalized;

            if (runSpeed.magnitude >= 0.1f)
            {
                faster = true;
                controller.transform.Translate(direction * run * Time.deltaTime);
            }

            else
            {
                faster = false;
            }
        }
    }

    else
    {
        movement = false;
    }

    Vector3 orientation = Vector3.forward * 1;
    Ray ray = new Ray(controller.transform.position, controller.transform.TransformDirection(orientation * speed));
    Debug.DrawRay(transform.position, transform.TransformDirection(orientation * prange));
    int layerMask = < 6;

    if (Physics.Raycast(ray, out RaycastHit hit))
    {
        if (Physics.Raycast(ray, out RaycastHit hit2, prange))
        {
            if (hit2.transform.tag == "Level")
            {
                print("I'm looking at " + hit.transform.name);

            }
        }

        else
        {
            print("I'm looking at nothing!");
        }
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ Did you consider using the raycast to detect whether there's a wall between you and your destination point before asking the controller to translate there, and skipping the translation line if that's the case? \$\endgroup\$
    – DMGregory
    Commented Aug 19, 2022 at 17:00
  • \$\begingroup\$ I did use the Raycast to detect it, in case my character, which has a Character Controller, hits the cube that has a Collider, it spams on the Console "I'm looking at" and depends on the cube I'm looking at. And if I do turn around looking at nothing, it does spam "I'm looking at nothing!"... I hope I got your question right \$\endgroup\$
    – mikeadam
    Commented Aug 19, 2022 at 17:03
  • \$\begingroup\$ The key part of my question was doing the raycast before transform.Translate. Then if you detect a wall, you can simply not translate into it. You may also want to use a shapecast like a CapsuleCast to account for the thickness of your character's collider. \$\endgroup\$
    – DMGregory
    Commented Aug 19, 2022 at 17:05
  • \$\begingroup\$ Oh no, I didn't, that's the entire code, the only Raycast I did was at the end. I did try to put all of the movement script inside of the Raycast code but to no avail. Also now that I realized, ignore that "layerMask" at the end, it's for something different. \$\endgroup\$
    – mikeadam
    Commented Aug 19, 2022 at 17:11
  • \$\begingroup\$ So I am trying to do some adjustments at this code, and I did try to do the raycast before the transform.Translate, but for some reason, everytime I rotate a certain angle or my character hits a collider, it just ends up stopping the character and that's it... could you tell me what you would do different in my code to detect that wall and stop the transform.Translate? \$\endgroup\$
    – mikeadam
    Commented Aug 19, 2022 at 23:52

0

You must log in to answer this question.

Browse other questions tagged .