#animations

1 messages · Page 1 of 1 (latest)

fallen marsh
#

using UnityEngine;

public class PlayerController : MonoBehaviour
{
// Player movement speed
public float walkSpeed = 5f;
public float sprintSpeed = 10f;
private float currentSpeed;

// Mouse sensitivity for looking around
public float mouseSensitivity = 2f;

// Vertical rotation limit for looking up and down
public float verticalRotationLimit = 60f;
private float verticalRotation = 0f;

// Player jump force
public float jumpForce = 5f;

// Player gravity
public float gravity = -9.8f;

// Character controller component
private CharacterController characterController;

// Player velocity
private Vector3 playerVelocity;

// Animator component
private Animator animator;

private void Start()
{
    // Get the CharacterController component
    characterController = GetComponent<CharacterController>();

    // Get the Animator component
    animator = GetComponent<Animator>();

    // Lock the cursor to the game window and hide it
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;

    // Set the initial speed to walk speed
    currentSpeed = walkSpeed;
}

private void Update()
{
    // Player movement
#

float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

    // Calculate the player's movement direction
    Vector3 movement = transform.forward * moveVertical + transform.right * moveHorizontal;
    movement.Normalize();

    // Apply movement to the character controller
    characterController.Move(movement * currentSpeed * Time.deltaTime);

    // Apply gravity to the player
    playerVelocity.y += gravity * Time.deltaTime;
    characterController.Move(playerVelocity * Time.deltaTime);

    // Player jumping
    if (characterController.isGrounded)
    {
        if (Input.GetButtonDown("Jump"))
        {
            // Add vertical force to make the player jump
            playerVelocity.y = Mathf.Sqrt(jumpForce * -2f * gravity);

            // Play jump animation
            animator.Play("Human Armature|Jump");
        }
        else
        {
            // Reset vertical velocity if the player is on the ground
            playerVelocity.y = -2f;

            // Determine the appropriate animation based on movement
            if (movement.magnitude > 0)
            {
                if (Input.GetKey(KeyCode.LeftShift))
                {
                    currentSpeed = sprintSpeed;
                    animator.Play("Human Armature|Run");
                }
                else
                {
                    currentSpeed = walkSpeed;
                    animator.Play("Human Armature|Walk");
                }
            }
            else
            {
                currentSpeed = 0f;
                animator.Play("Human Armature|Idle");
            }
        }
#

}

    // Player looking around
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
    float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;

    // Rotate the player horizontally
    transform.Rotate(0f, mouseX, 0f);

    // Rotate the main camera vertically
    verticalRotation -= mouseY;
    verticalRotation = Mathf.Clamp(verticalRotation, -verticalRotationLimit, verticalRotationLimit);
    Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0f, 0f);

    // Player punching
    if (Input.GetMouseButtonDown(0))
    {
        // Play punch animation
        animator.Play("Human Armature|Punch");
    }
}

}

thorn kraken
#

you have to go into the animation window in untiy

#

click your animation

#

and check looping

#

or loop

#

its called sth like this

#

@fallen marsh did it work?