Templevrun-2
unknown
c_cpp
a year ago
6.9 kB
9
Indexable
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 10f;
public float laneDistance = 4f; // Distance between lanes
public float jumpForce = 7f;
private Rigidbody rb;
private bool isGrounded = true;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
// Move forward constantly
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
// Handle lane switching
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
{
if (transform.position.x > -laneDistance)
transform.position = new Vector3(transform.position.x - laneDistance, transform.position.y, transform.position.z);
}
else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
{
if (transform.position.x < laneDistance)
transform.position = new Vector3(transform.position.x + laneDistance, transform.position.y, transform.position.z);
}
// Handle jumping
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
void OnCollisionEnter(Collision collision)
{
// If the player touches the ground, they can jump again
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
}
using UnityEngine;
public class ObstacleSpawner : MonoBehaviour
{
public GameObject obstaclePrefab;
public float spawnDistance = 20f;
public float spawnInterval = 2f;
private float lastSpawnTime = 0f;
void Update()
{
if (Time.time - lastSpawnTime >= spawnInterval)
{
SpawnObstacle();
lastSpawnTime = Time.time;
}
}
void SpawnObstacle()
{
float randomX = Random.Range(-4f, 4f);
Vector3 spawnPosition = new Vector3(randomX, 0.5f, transform.position.z + spawnDistance);
Instantiate(obstaclePrefab, spawnPosition, Quaternion.identity);
}
}
using UnityEngine;
public class EndlessTrack : MonoBehaviour
{
public GameObject trackPrefab;
public float trackLength = 30f;
private Transform playerTransform;
private Vector3 nextTrackPosition;
void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
nextTrackPosition = transform.position;
}
void Update()
{
if (playerTransform.position.z > nextTrackPosition.z - trackLength * 2)
{
SpawnTrack();
}
}
void SpawnTrack()
{
nextTrackPosition.z += trackLength;
Instantiate(trackPrefab, nextTrackPosition, Quaternion.identity);
}
}
using UnityEngine;
public class GameManager : MonoBehaviour
{
public bool gameOver = false;
public int score = 0;
void Update()
{
if (gameOver)
{
// Display Game Over UI
Debug.Log("Game Over! Your Score: " + score);
}
}
public void EndGame()
{
gameOver = true;
Time.timeScale = 0; // Freeze the game
}
public void IncreaseScore(int amount)
{
score += amount;
Debug.Log("Score: " + score);
}
}using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 10f;
public float jumpForce = 7f;
public float laneDistance = 4f;
private Rigidbody rb;
private Animator animator;
private bool isGrounded = true;
void Start()
{
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}
void Update()
{
// Move the player forward
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
// Set running speed to the animator
animator.SetFloat("Speed", moveSpeed);
// Handle lane switching
if (Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.A))
{
if (transform.position.x > -laneDistance)
transform.position = new Vector3(transform.position.x - laneDistance, transform.position.y, transform.position.z);
}
else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.D))
{
if (transform.position.x < laneDistance)
transform.position = new Vector3(transform.position.x + laneDistance, transform.position.y, transform.position.z);
}
// Handle jumping
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
isGrounded = false;
// Trigger jump animation
animator.SetBool("isJumping", true);
}
// Handle sliding
if (Input.GetKeyDown(KeyCode.LeftControl))
{
// Trigger slide animation
animator.SetBool("isSliding", true);
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
isGrounded = true;
// Reset jump and slide animations when back on ground
animator.SetBool("isJumping", false);
animator.SetBool("isSliding", false);
}
}
}
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public AudioClip jumpSound;
public AudioClip coinSound;
private AudioSource audioSource;
void Start()
{
// Get the AudioSource component attached to the player
audioSource = GetComponent<AudioSource>();
}
void Update()
{
// Example for jumping sound
if (Input.GetKeyDown(KeyCode.Space))
{
// Play jump sound
PlaySound(jumpSound);
}
}
void OnTriggerEnter(Collider other)
{
// Example for coin collection sound
if (other.gameObject.tag == "Coin")
{
// Play coin collection sound
PlaySound(coinSound);
Destroy(other.gameObject); // Destroy the coin
}
}
// Function to play a sound
void PlaySound(AudioClip clip)
{
audioSource.PlayOneShot(clip);
}
}
using UnityEngine;
public class MusicManager : MonoBehaviour
{
private AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
audioSource.Play(); // Start playing music
}
public void StopMusic()
{
audioSource.Stop(); // Stop the music
}
public void PauseMusic()
{
audioSource.Pause(); // Pause the music
}
public void ResumeMusic()
{
audioSource.UnPause(); // Resume the music after pause
}
}
audioSource.volume = 0.5f; // Set volume to 59%Editor is loading...
Leave a Comment