Advertisement
MatiGe

Untitled

Jul 7th, 2024
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Tank : MonoBehaviour
  6. {
  7.     public float speed = 10f;
  8.     private CharacterController cc;
  9.     public float momentumDamping = 5f;
  10.  
  11.     private Vector3 inputVector;
  12.     private Vector3 moveVector;
  13.     private float gravity = -10f;
  14.  
  15.  
  16.     [SerializeField] GameObject parti;
  17.  
  18.     public GameObject[] spawn;
  19.  
  20.     public GameObject p;
  21.     // Start is called before the first frame update
  22.     void Start()
  23.     {
  24.         cc = GetComponent<CharacterController>();
  25.  
  26.        
  27.         spawn = GameObject.FindGameObjectsWithTag("Spawn");
  28.  
  29.        
  30.            
  31.     }
  32.  
  33.     // Update is called once per frame
  34.     void Update()
  35.     {
  36.         GetInput();
  37.         MovePlayer();
  38.     }
  39.  
  40.     void GetInput()
  41.     {
  42.         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
  43.         {
  44.             inputVector = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
  45.             inputVector.Normalize();
  46.             inputVector = transform.TransformDirection(inputVector);
  47.  
  48.            
  49.         }
  50.         else
  51.         {
  52.             inputVector = Vector3.Lerp(inputVector, Vector3.zero, momentumDamping * Time.deltaTime);
  53.         }
  54.  
  55.  
  56.  
  57.  
  58.         moveVector = (inputVector * speed) + (Vector3.up * gravity);
  59.     }
  60.  
  61.     void MovePlayer()
  62.     {
  63.         cc.Move(moveVector * Time.deltaTime);
  64.     }
  65.  
  66.     void Respawn(GameObject pl)
  67.     {
  68.        
  69.  
  70.         // Choose a random spawn point from the array
  71.         int randomIndex = Random.Range(0, 15);
  72.         GameObject chosenSpawnPoint = spawn[randomIndex];
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.         p.transform.position = chosenSpawnPoint.transform.position;
  80.  
  81.  
  82.     }
  83.  
  84.  
  85.     private void OnTriggerEnter(Collider other)
  86.     {
  87.         if (other.gameObject.CompareTag("Ball"))
  88.         {
  89.            
  90.  
  91.             GameObject explosion = Instantiate(parti, transform.position, transform.rotation);
  92.  
  93.             Respawn(p);
  94.  
  95.             Destroy(explosion, 0.75f);
  96.  
  97.         }
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement