0% found this document useful (0 votes)
4 views1 page

Using UnityEngine

This Unity script controls player movement, allowing for movement in horizontal and vertical directions, mouse-based rotation, and jumping. It uses a Rigidbody component to manage physics interactions and includes a check to determine if the player is grounded before allowing jumps. The script also prevents the player from tipping over upon collision by freezing rotation.

Uploaded by

Higor Bonfim
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Using UnityEngine

This Unity script controls player movement, allowing for movement in horizontal and vertical directions, mouse-based rotation, and jumping. It uses a Rigidbody component to manage physics interactions and includes a check to determine if the player is grounded before allowing jumps. The script also prevents the player from tipping over upon collision by freezing rotation.

Uploaded by

Higor Bonfim
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMovement : MonoBehaviour
{
public float speed = 10f; // Velocidade de movimento
public float mouseSensitivity = 200f; // Sensibilidade do mouse
public float jumpForce = 5f; // For�a do pulo

private float rotationY = 0f; // Rota��o horizontal


private Rigidbody rb;
private bool isGrounded = true; // Verifica se est� no ch�o

void Start()
{
rb = GetComponent<Rigidbody>();
[Link] = true; // Impede que o player tombe ao colidir
}

void Update()
{
// --- Movimento ---
float moveX = [Link]("Horizontal");
float moveZ = [Link]("Vertical");

Vector3 move = [Link] * moveX + [Link] * moveZ;


Vector3 moveVelocity = move * speed;
[Link] = new Vector3(moveVelocity.x, [Link].y,
moveVelocity.z);

// --- Rota��o com o mouse ---


float mouseX = [Link]("Mouse X") * mouseSensitivity * [Link];
rotationY += mouseX;
[Link] = [Link](0f, rotationY, 0f);

// --- Pulo (barra de espa�o) ---


if ([Link]([Link]) && isGrounded)
{
[Link]([Link] * jumpForce, [Link]);
isGrounded = false;
}
}

// Detecta se o player voltou ao ch�o


void OnCollisionEnter(Collision collision)
{
if ([Link]("Ground"))
{
isGrounded = true;
}
}
}

You might also like