че та
This commit is contained in:
parent
de6fdb1e47
commit
6e3d5ab9a2
8
Assets/Scripts.meta
Normal file
8
Assets/Scripts.meta
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1037018fb69caf541b3dfb87aa3bbbc5
|
||||||
|
folderAsset: yes
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
72
Assets/Scripts/PlayerMovement.cs
Normal file
72
Assets/Scripts/PlayerMovement.cs
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
[RequireComponent(typeof(CharacterController))]
|
||||||
|
public class PlayerMovement : MonoBehaviour
|
||||||
|
{
|
||||||
|
public float speed = 7.5f;
|
||||||
|
public float sprintSpeed = 11.5f;
|
||||||
|
public float jumpSpeed = 8.0f;
|
||||||
|
public float gravity = 20.0f;
|
||||||
|
public Camera playerCamera;
|
||||||
|
public float lookSpeed = 2.0f;
|
||||||
|
public float lookXLimit = 45.0f;
|
||||||
|
|
||||||
|
private CharacterController characterController;
|
||||||
|
private Vector3 moveDirection = Vector3.zero;
|
||||||
|
private float rotationX = 0;
|
||||||
|
|
||||||
|
[HideInInspector]
|
||||||
|
public bool canMove = true;
|
||||||
|
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
characterController = GetComponent<CharacterController>();
|
||||||
|
|
||||||
|
// Lock cursor
|
||||||
|
Cursor.lockState = CursorLockMode.Locked;
|
||||||
|
Cursor.visible = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
// We are grounded, so recalculate move direction based on axes
|
||||||
|
Vector3 forward = transform.TransformDirection(Vector3.forward);
|
||||||
|
Vector3 right = transform.TransformDirection(Vector3.right);
|
||||||
|
|
||||||
|
// Press Left Shift to sprint
|
||||||
|
bool isSprinting = Input.GetKey(KeyCode.LeftShift);
|
||||||
|
float curSpeedX = canMove ? (isSprinting ? sprintSpeed : speed) * Input.GetAxis("Vertical") : 0;
|
||||||
|
float curSpeedY = canMove ? (isSprinting ? sprintSpeed : speed) * Input.GetAxis("Horizontal") : 0;
|
||||||
|
float movementDirectionY = moveDirection.y;
|
||||||
|
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
|
||||||
|
|
||||||
|
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
|
||||||
|
{
|
||||||
|
moveDirection.y = jumpSpeed;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
moveDirection.y = movementDirectionY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
|
||||||
|
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
|
||||||
|
// as an acceleration (ms^-2)
|
||||||
|
if (!characterController.isGrounded)
|
||||||
|
{
|
||||||
|
moveDirection.y -= gravity * Time.deltaTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move the controller
|
||||||
|
characterController.Move(moveDirection * Time.deltaTime);
|
||||||
|
|
||||||
|
// Player and Camera rotation
|
||||||
|
if (canMove)
|
||||||
|
{
|
||||||
|
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
|
||||||
|
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
|
||||||
|
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
|
||||||
|
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Assets/Scripts/PlayerMovement.cs.meta
Normal file
2
Assets/Scripts/PlayerMovement.cs.meta
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 0dd0f6132788a3e45b5289b710fdc8fb
|
||||||
81
Assets/Scripts/PlayerShooting.cs
Normal file
81
Assets/Scripts/PlayerShooting.cs
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class PlayerShooting : MonoBehaviour
|
||||||
|
{
|
||||||
|
public int damagePerShot = 20;
|
||||||
|
public float timeBetweenBullets = 0.15f;
|
||||||
|
public float range = 100f;
|
||||||
|
|
||||||
|
float timer;
|
||||||
|
Ray shootRay = new Ray();
|
||||||
|
RaycastHit shootHit;
|
||||||
|
int shootableMask;
|
||||||
|
ParticleSystem gunParticles;
|
||||||
|
LineRenderer gunLine;
|
||||||
|
AudioSource gunAudio;
|
||||||
|
Light gunLight;
|
||||||
|
float effectsDisplayTime = 0.2f;
|
||||||
|
|
||||||
|
void Awake()
|
||||||
|
{
|
||||||
|
shootableMask = LayerMask.GetMask("Shootable");
|
||||||
|
gunParticles = GetComponent<ParticleSystem>();
|
||||||
|
gunLine = GetComponent<LineRenderer>();
|
||||||
|
gunAudio = GetComponent<AudioSource>();
|
||||||
|
gunLight = GetComponent<Light>();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
timer += Time.deltaTime;
|
||||||
|
|
||||||
|
if (Input.GetButton("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
|
||||||
|
{
|
||||||
|
Shoot();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (timer >= timeBetweenBullets * effectsDisplayTime)
|
||||||
|
{
|
||||||
|
DisableEffects();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DisableEffects()
|
||||||
|
{
|
||||||
|
gunLine.enabled = false;
|
||||||
|
gunLight.enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Shoot()
|
||||||
|
{
|
||||||
|
timer = 0f;
|
||||||
|
|
||||||
|
gunAudio.Play();
|
||||||
|
|
||||||
|
gunLight.enabled = true;
|
||||||
|
|
||||||
|
gunParticles.Stop();
|
||||||
|
gunParticles.Play();
|
||||||
|
|
||||||
|
gunLine.enabled = true;
|
||||||
|
gunLine.SetPosition(0, transform.position);
|
||||||
|
|
||||||
|
shootRay.origin = transform.position;
|
||||||
|
shootRay.direction = transform.forward;
|
||||||
|
|
||||||
|
if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
|
||||||
|
{
|
||||||
|
// We hit a shootable object
|
||||||
|
Target target = shootHit.collider.GetComponent<Target>();
|
||||||
|
if (target != null)
|
||||||
|
{
|
||||||
|
target.TakeDamage(damagePerShot);
|
||||||
|
}
|
||||||
|
gunLine.SetPosition(1, shootHit.point);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Assets/Scripts/PlayerShooting.cs.meta
Normal file
2
Assets/Scripts/PlayerShooting.cs.meta
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9d58a8551ce33d64fb4d691591a5bcea
|
||||||
21
Assets/Scripts/Target.cs
Normal file
21
Assets/Scripts/Target.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class Target : MonoBehaviour
|
||||||
|
{
|
||||||
|
public float health = 50f;
|
||||||
|
|
||||||
|
public void TakeDamage(float amount)
|
||||||
|
{
|
||||||
|
health -= amount;
|
||||||
|
if (health <= 0f)
|
||||||
|
{
|
||||||
|
Die();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Die()
|
||||||
|
{
|
||||||
|
// For now, we'll just destroy the target object
|
||||||
|
Destroy(gameObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
2
Assets/Scripts/Target.cs.meta
Normal file
2
Assets/Scripts/Target.cs.meta
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 90b9cda00fa7d9d4d99d69691e8b4f12
|
||||||
Loading…
Reference in New Issue
Block a user