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

Coding

The document contains three Unity scripts: PlayerController, CameraFollow, and BlockManager. PlayerController handles player movement, jumping, and respawning; CameraFollow smoothly follows the player; and BlockManager manages block placement, rotation, and deletion. Together, these scripts facilitate a basic game environment with player interaction and camera dynamics.

Uploaded by

jahiduljawad49
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)
8 views3 pages

Coding

The document contains three Unity scripts: PlayerController, CameraFollow, and BlockManager. PlayerController handles player movement, jumping, and respawning; CameraFollow smoothly follows the player; and BlockManager manages block placement, rotation, and deletion. Together, these scripts facilitate a basic game environment with player interaction and camera dynamics.

Uploaded by

jahiduljawad49
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;

public class PlayerController : MonoBehaviour


{
public float speed = 5f;
public float jumpForce = 7f;
private Rigidbody rb;
private bool isGrounded;
private Vector3 spawnPoint;

void Start()
{
rb = GetComponent<Rigidbody>();
spawnPoint = [Link]; // initial spawn
}

void Update()
{
// Movement
float moveX = [Link]("Horizontal");
float moveZ = [Link]("Vertical");
Vector3 move = [Link] * moveX + [Link] * moveZ;
Vector3 velocity = move * speed;
velocity.y = [Link].y;
[Link] = velocity;

// Jump
if ([Link]([Link]) && isGrounded)
{
[Link]([Link] * jumpForce, [Link]);
}

// Respawn if fall
if ([Link].y < -10f)
{
Respawn();
}
}

private void OnCollisionStay(Collision collision)


{
isGrounded = [Link]("Ground");
}
void Respawn()
{
[Link] = [Link];
[Link] = spawnPoint;
}
}using UnityEngine;

public class CameraFollow : MonoBehaviour


{
public Transform target;
public Vector3 offset = new Vector3(0, 5, -7);
public float smoothSpeed = 0.125f;

void LateUpdate()
{
Vector3 desiredPos = [Link] + offset;
Vector3 smoothedPos = [Link]([Link], desiredPos,
smoothSpeed);
[Link] = smoothedPos;

[Link](target);
}
}using UnityEngine;

public class BlockManager : MonoBehaviour


{
public GameObject[] blockPrefabs;
public float placeDistance = 5f;
private int selectedBlock = 0;

void Update()
{
// Inventory scroll
if ([Link].y != 0)
{
selectedBlock = (selectedBlock + (int)[Link].y) %
[Link];
if (selectedBlock < 0) selectedBlock += [Link];
}

// Place block
if ([Link](0))
{
Vector3 placePos = [Link] + [Link] * placeDis -
tance;
Instantiate(blockPrefabs[selectedBlock], placePos, [Link]-
tity);
}

// Rotate block
if ([Link](1)) // Right click
{
Vector3 placePos = [Link] + [Link] * placeDis -
tance;
GameObject block = Instantiate(blockPrefabs[selectedBlock], place-
Pos, [Link]);
[Link](0, 90, 0);
}

// Delete block
if ([Link](KeyCode.E))
{
Ray ray = new Ray([Link], [Link]);
if ([Link](ray, out RaycastHit hit, placeDistance))
{
if ([Link]("Block"))
{
Destroy([Link]);
}
}
}
}
}

You might also like