Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Tank : MonoBehaviour
- {
- public float speed = 10f;
- private CharacterController cc;
- public float momentumDamping = 5f;
- private Vector3 inputVector;
- private Vector3 moveVector;
- private float gravity = -10f;
- [SerializeField] GameObject parti;
- public GameObject[] spawn;
- public GameObject p;
- // Start is called before the first frame update
- void Start()
- {
- cc = GetComponent<CharacterController>();
- spawn = GameObject.FindGameObjectsWithTag("Spawn");
- }
- // Update is called once per frame
- void Update()
- {
- GetInput();
- MovePlayer();
- }
- void GetInput()
- {
- if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
- {
- inputVector = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
- inputVector.Normalize();
- inputVector = transform.TransformDirection(inputVector);
- }
- else
- {
- inputVector = Vector3.Lerp(inputVector, Vector3.zero, momentumDamping * Time.deltaTime);
- }
- moveVector = (inputVector * speed) + (Vector3.up * gravity);
- }
- void MovePlayer()
- {
- cc.Move(moveVector * Time.deltaTime);
- }
- void Respawn(GameObject pl)
- {
- // Choose a random spawn point from the array
- int randomIndex = Random.Range(0, 15);
- GameObject chosenSpawnPoint = spawn[randomIndex];
- p.transform.position = chosenSpawnPoint.transform.position;
- }
- private void OnTriggerEnter(Collider other)
- {
- if (other.gameObject.CompareTag("Ball"))
- {
- GameObject explosion = Instantiate(parti, transform.position, transform.rotation);
- Respawn(p);
- Destroy(explosion, 0.75f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement