Keeping Crouch

rpg_s01_gamepad_movement_mode

#1

How would I get Crouch to work in the new PlayerMovement script?

I was able to get Jumping to work but not able to get Crouch, working, what am i missing?


#2

Show your script.


#3

@Mattias_Olgerfelt

Here is my PlayerMovement script.

using System;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;

[RequireComponent(typeof(ThirdPersonCharacter))]
public class PlayerMovement : MonoBehaviour
{
    
    [SerializeField] float walkMoveStopRadius = 0.2f;
    [SerializeField] float attackMoveStopRadius = 5f;


    ThirdPersonCharacter ThirdPersonCharacter;   // A reference to the ThirdPersonCharacter on the object
    CameraRaycaster cameraRaycaster;
    Vector3 currentDestination, clickPoint;

    private bool m_Jump;                      // the world-relative desired move direction, calculated from the camForward and user input.
    bool isInDirectMode = false;

    private void Start()
    {
        cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
        ThirdPersonCharacter = GetComponent<ThirdPersonCharacter>();
        currentDestination = transform.position;
    }

    // Fixed update is called in sync with physics
    private void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.G)) // Toggle Betwen Modes 
        {
            isInDirectMode = !isInDirectMode;
            currentDestination = transform.position; // Clear Click target
        }
        if (isInDirectMode)
        {
            ProcessDirectMovement();

        }
        else
        {
            ProcessMouseMovement();
        }

        ProcessMouseMovement(); // Process Mouse Movement

    }

    private void ProcessDirectMovement()
    {
        // read inputs
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        bool crouch = Input.GetKey(KeyCode.C);

        // calculate camera relative direction to move:
        Vector3 cameraForward = Vector3.Scale(Camera.main.transform.forward, new Vector3(1, 0, 1)).normalized;
        Vector3 movement = v * cameraForward + h * Camera.main.transform.right;

        m_Jump = Input.GetButtonDown("Jump");
        
     
        ThirdPersonCharacter.Move(movement, crouch, m_Jump);
        //m_Jump = false;
    }


    private void ProcessMouseMovement()
    {
        if (Input.GetMouseButton(0))
        {
            print("Cursor raycast hit layer: " + cameraRaycaster.currentLayerHit);

            clickPoint = cameraRaycaster.hit.point;
            switch (cameraRaycaster.currentLayerHit)
            {
                case Layer.Walkable:
                    currentDestination = ShortDestination(clickPoint, walkMoveStopRadius);
                    break;
                case Layer.Enemy:
                    currentDestination = ShortDestination(clickPoint, attackMoveStopRadius);

                    print("Not moving to enemy");
                    break;
                default:
                    print("Unexpected layer found");
                    return;
            }

        }
        WalkToDestination();

    }

    private void WalkToDestination()
    {
        var playerToClickPoint = currentDestination - transform.position;
        if (playerToClickPoint.magnitude >= walkMoveStopRadius)
        {
            ThirdPersonCharacter.Move(playerToClickPoint, false, false);
        }
        else
        {
            ThirdPersonCharacter.Move(Vector3.zero, false, false);
            currentDestination = transform.position;
        }
    }

    Vector3 ShortDestination(Vector3 destination, float shortening)
    {
        Vector3 reductionVector = (destination - transform.position).normalized * shortening;
        return destination - reductionVector;
    }

        void OnDrawGizmos()
        {
        Gizmos.color = Color.black;
        Gizmos.DrawLine(transform.position, currentDestination);
        Gizmos.DrawSphere(currentDestination, 0.1f);
        Gizmos.DrawSphere(clickPoint, 0.15f);

        //Draw
        Gizmos.color = new Color(255f, 0f, .5f);
        Gizmos.DrawWireSphere(transform.position, attackMoveStopRadius);
    }

}