| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- [SerializeField]
- private float powerLevel;
- public float PowerLevel
- {
- get
- {
- return powerLevel;
- }
- set
- {
- powerLevel = value;
- if (powerLevel > powerLevelMax)
- {
- powerLevel = powerLevelMax;
- }
- }
- }
- public float startSpeed = 5f;
- public float lookSmoothingSeconds = 0.05f;
- public float cameraSmoothingSeconds = 1;
- public float bulletUpBias = 0.1f;
- public float bulletForce = 10f;
- public float powerLevelMax = 8.0f;
- public Transform gunTransform;
- public Rigidbody bulletPrefab;
- public Light muzzleLight;
- private Vector3 inputDirection;
- private float speed;
- private Vector3 lookDirection;
- private float deadZone = 0.1f;
- private Vector3 lookVelocity;
- private Vector3 cameraOffset;
- private Vector3 cameraVelocity;
- private bool shooting = false;
- private float lightIntensity;
- // Use this for initialization
- void Start ()
- {
- speed = startSpeed;
- cameraOffset = Camera.main.transform.position - transform.position;
- lightIntensity = muzzleLight.intensity;
- }
-
- // Update is called once per frame
- void Update ()
- {
- // TODO lerp movement direction
- GetInput();
- if (lookDirection.magnitude > deadZone)
- transform.forward = Vector3.SmoothDamp(transform.forward, lookDirection, ref lookVelocity, lookSmoothingSeconds);
- transform.position += inputDirection * speed * Time.deltaTime;
- Camera.main.transform.position = Vector3.SmoothDamp(Camera.main.transform.localPosition, TargetCameraPosition(), ref cameraVelocity, cameraSmoothingSeconds);
- if (shooting)
- {
- if (Time.frameCount % 2 == 0)
- {
- muzzleLight.intensity = 0;
- }
- else
- {
- muzzleLight.intensity = Random.Range(.5f,2f) * lightIntensity;
- }
- Shoot();
- }
- else
- {
- muzzleLight.intensity = 0;
- }
- }
- private void Shoot()
- {
- var rb = Instantiate<Rigidbody>(bulletPrefab);
- rb.transform.position = gunTransform.position;
- rb.transform.forward = gunTransform.forward;
- rb.AddForce(bulletForce * (transform.forward + Vector3.up * bulletUpBias), ForceMode.VelocityChange);
- Destroy(rb.gameObject, 2f);
- }
- private Vector3 TargetCameraPosition()
- {
- return transform.position + cameraOffset;
- }
- void GetInput()
- {
- shooting = Input.GetButton("Fire1");
- lookDirection = new Vector3(
- Input.GetAxis("LookX"),
- 0,
- -Input.GetAxis("LookY")
- ).normalized;
- inputDirection = new Vector3(
- Input.GetAxis("Horizontal"),
- 0,
- Input.GetAxis("Vertical")
- ).normalized;
- }
- }
|