Untitled
unknown
plain_text
9 months ago
7.4 kB
13
Indexable
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public GameObject bulletPrefab;
public Transform firePoint;
public float bulletSpeed = 20f;
void Update()
{
// Movement (WASD / Arrow keys)
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 move = new Vector3(h, 0, v);
transform.Translate(move * moveSpeed * Time.deltaTime, Space.World);
// Rotate toward mouse or forward
if (move != Vector3.zero)
transform.forward = move;
// Shoot (space bar)
if (Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}
}
void Shoot()
{
if (bulletPrefab && firePoint)
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody rb = bullet.GetComponent<Rigidbody>();
if (rb)
{
rb.velocity = firePoint.forward * bulletSpeed;
}
Destroy(bullet, 3f); // Auto destroy after 3 seconds
}
}
}using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f;
public GameObject bulletPrefab;
public Transform firePoint;
public float bulletSpeed = 20f;
void Update()
{
// Movement (WASD / Arrow keys)
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 move = new Vector3(h, 0, v);
transform.Translate(move * moveSpeed * Time.deltaTime, Space.World);
// Rotate toward mouse or forward
if (move != Vector3.zero)
transform.forward = move;
// Shoot (space bar)
if (Input.GetKeyDown(KeyCode.Space))
{
Shoot();
}
}
void Shoot()
{
if (bulletPrefab && firePoint)
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody rb = bullet.GetComponent<Rigidbody>();
if (rb)
{
rb.velocity = firePoint.forward * bulletSpeed;
}
Destroy(bullet, 3f); // Auto destroy after 3 seconds
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}Editor is loading...
Leave a Comment