Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views3 pages

Move 2

Uploaded by

danton.avt2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Move 2

Uploaded by

danton.avt2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using UnityEngine;

public class PlayerMovement3D : MonoBehaviour


{
public float walkSpeed = 5f;
public float runSpeed = 10f;
public float crouchSpeed = 2.5f;
public float jumpForce = 5f;
public float gravityMultiplier = 2f;

private Rigidbody rb;


private Vector3 movement;
private bool isGrounded;
private bool isCrouching;

void Start()
{
rb = GetComponent<Rigidbody>();
Physics.gravity *= gravityMultiplier; // Aumenta a força da gravidade
}

void Update()
{
// Captura a entrada do teclado
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

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


{
Jump();
}

if (Input.GetKeyDown(KeyCode.LeftShift))
{
Run();
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
Walk();
}

if (Input.GetKeyDown(KeyCode.C))
{
Crouch();
}
else if (Input.GetKeyUp(KeyCode.C))
{
StandUp();
}

if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}

void FixedUpdate()
{
// Move o jogador
Vector3 move = transform.right * movement.x + transform.forward *
movement.z;
rb.MovePosition(rb.position + move * GetCurrentSpeed() *
Time.fixedDeltaTime);
}

void Jump()
{
rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
isGrounded = false;
}

void Run()
{
moveSpeed = runSpeed;
}

void Walk()
{
moveSpeed = walkSpeed;
}

void Crouch()
{
isCrouching = true;
moveSpeed = crouchSpeed;
// Ajusta a altura do jogador para parecer agachado
transform.localScale = new Vector3(transform.localScale.x,
transform.localScale.y / 2, transform.localScale.z);
}

void StandUp()
{
isCrouching = false;
moveSpeed = walkSpeed;
// Restaura a altura do jogador
transform.localScale = new Vector3(transform.localScale.x,
transform.localScale.y * 2, transform.localScale.z);
}

void Shoot()
{
// Implementação do disparo, ex: instanciar um projétil
Debug.Log("Bang!");
}

float GetCurrentSpeed()
{
return isCrouching ? crouchSpeed : (Input.GetKey(KeyCode.LeftShift) ?
runSpeed : walkSpeed);
}

private void OnCollisionStay(Collision collision)


{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
}

You might also like