Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityStandardAssets.CrossPlatformInput;
- public class PlayerController : MonoBehaviour {
- [Header("General")]
- [Tooltip("In ms^-1")][SerializeField] float controlSpeed = 10f;
- [Tooltip("In m1")] [SerializeField] float xRange = 5f;
- [Tooltip("In m1")] [SerializeField] float yRange = 5f;
- [SerializeField] GameObject[] Guns;
- [Header("Screen-position Based")]
- [SerializeField] float positionPitchFactor = -2f;
- [SerializeField] float positionYawFactor = 4f;
- [Header("Control-throw Based")]
- [SerializeField] float controlPitchFactor = -15f;
- [SerializeField] float controlRollFactor = -20f;
- float xThrow, yThrow;
- bool isControlEnabled = true;
- // Use this for initialization
- // Update is called once per frame
- void Update ()
- {
- if (isControlEnabled)
- {
- ProcessTranslation();
- ProcessRotation();
- ProcessFiring();
- }
- }
- void die()
- {
- isControlEnabled = false;
- }
- void OnPlayerDeath() // called by string reference
- {
- Invoke("die", 10);
- }
- private void ProcessRotation()
- {
- float pitchDueToPosition = transform.localPosition.y * positionPitchFactor;
- float pitchDueToControlThrow = yThrow * controlPitchFactor;
- float pitch = pitchDueToPosition + pitchDueToControlThrow;
- float yaw = transform.localPosition.x * positionYawFactor;
- float roll = xThrow * controlRollFactor;
- transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
- }
- private void ProcessTranslation()
- {
- xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
- yThrow = CrossPlatformInputManager.GetAxis("Vertical");
- float xOffset = xThrow * controlSpeed * Time.deltaTime;
- float yOffset = yThrow * controlSpeed * Time.deltaTime;
- float rawXPos = transform.localPosition.x + xOffset;
- float clampedXPos = Mathf.Clamp(rawXPos, -xRange, xRange);
- float rawYPos = transform.localPosition.y + yOffset;
- float clampedYPos = Mathf.Clamp(rawYPos, -yRange, yRange);
- transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
- }
- void ProcessFiring()
- {
- if (CrossPlatformInputManager.GetButton("fire"))
- {
- SetGunsActive(true);
- }
- else
- {
- SetGunsActive(false);
- }
- }
- private void SetGunsActive(bool isActive)
- {
- foreach(GameObject gun in Guns)
- {
- var emmisionModual = gun.GetComponent<ParticleSystem>().emission;
- emmisionModual.enabled = isActive;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement