| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Player : MonoBehaviour
- {
- public float startSpeed = 5f;
- public float lookSmoothingSeconds = 0.05f;
- public float cameraSmoothingSeconds = 1;
- public float bulletUpBias = 0.1f;
- public float bulletForce = 10f;
- public Transform gunTransform;
- public Rigidbody bulletPrefab;
- 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;
- // Use this for initialization
- void Start ()
- {
- speed = startSpeed;
- cameraOffset = Camera.main.transform.position - transform.position;
- }
-
- // 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)
- {
- Shoot();
- }
- }
- 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.Impulse);
- 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;
- }
- }
|