Untitled
unknown
plain_text
5 months ago
1.2 kB
1
Indexable
using UnityEngine; public class PlayerController : MonoBehaviour { public float speed = 5.0f; public float rotationSpeed = 200.0f; void Update() { float translation = Input.GetAxis("Vertical") * speed; float rotation = Input.GetAxis("Horizontal") * rotationSpeed; translation *= Time.deltaTime; rotation *= Time.deltaTime; transform.Translate(0, 0, translation); transform.Rotate(0, rotation, 0); } } using UnityEngine; public class PlayerShooting : MonoBehaviour { public GameObject bulletPrefab; public Transform bulletSpawn; public float bulletSpeed = 20.0f; void Update() { if (Input.GetButtonDown("Fire1")) { Fire(); } } void Fire() { // Create the bullet from the prefab GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation); // Add velocity to the bullet Rigidbody rb = bullet.GetComponent<Rigidbody>(); rb.velocity = bulletSpawn.forward * bulletSpeed; // Destroy the bullet after 2 seconds Destroy(bullet, 2.0f); } }
Editor is loading...
Leave a Comment