1
\$\begingroup\$

I'm trying to make my first game in Unity: a 3d platformer inspired by the classic Crash Bandicoot games.

I've managed to make most of the movement with the character control script below. However, I don't know how I can do a slide/crouch.

What I want to do is to rotate and move the player down closer to the ground and give him some more speed that decreases over time until he reaches a normal waking state.

this is basically the motion I want to do but, but in all the axis

I thought about making the player rotate on a specific axis but when he turns the axis doesn't change so now I'm kind of lost in what to do.

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public CharacterController controller;

    public float speed = 10f;

    //turnning smoothness

    public float turnSmoothTime = 0.1f;
    float turnSmoothVelocity;

    //jump controls

    public float jumpHeight = 3f;
    public float gravity = -29.43f;
    Vector3 velocity;
    bool isGrounded;

    //gravity check
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if(isGrounded && velocity.y < 0)
        {
            velocity.y = -1f;
        }

       //player movement on both axis

        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        
        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
       
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);

        if (Input.GetKeyDown("space") && isGrounded)
        {
            velocity.y += Mathf.Sqrt(jumpHeight * -2f * gravity);
        }
        
        if (direction.magnitude >= 0.1f)
        {
            //player turning depending on where moving

            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime); 
            transform.rotation = Quaternion.Euler(0f, angle, 0f);
            
            controller.Move(direction * speed * Time.deltaTime);
            
        }
            
    }
}
\$\endgroup\$
8
  • \$\begingroup\$ Can you show us the specific rotation you want to perform, the code/animation data setup you've applied to try to perform it, and the result you observe in game so we can understand the specific issue? \$\endgroup\$ Commented Jan 11, 2021 at 18:46
  • \$\begingroup\$ just updated the post with the image of what I want to do but I still don't know the code on how it would be possible to do this that I'm thinking of \$\endgroup\$ Commented Jan 11, 2021 at 18:55
  • \$\begingroup\$ because lets say if the character is looking at a 45 degree angle and the player uses the button to slide he character should slide in the direction that the character is facing but I still don't know how to implement that into the code \$\endgroup\$ Commented Jan 11, 2021 at 18:57
  • \$\begingroup\$ the crouching/rotation would probably be done with an animation and then the speed would be done in code. At least that is how I would do it. \$\endgroup\$ Commented Jan 11, 2021 at 20:08
  • \$\begingroup\$ how would you code the speed so that it would decrease over time? \$\endgroup\$ Commented Jan 11, 2021 at 20:16

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.