[DUVIDA]otimizaçao de script
Página 1 de 1
[DUVIDA]otimizaçao de script
quero saber se eu utilizar o script desta forma eh mais otimizada,um script só pra comandos e outro as funçoes
- Código:
using UnityEngine;
public class InputController : MonoBehaviour
{
public float sensyX = 50;
public float sensyY = 50;
private float horizontal;
private float vertical;
private float mouseX;
private float mouseY;
private bool backTimeButton;
private bool stopTimeButton;
private bool jumpButton;
private bool pickUpButton;
private bool runButton;
private bool pauseButton;
private bool cancel;
private Player player;
private GameObject[] backTime;
private GameManager gameManager;
void Awake()
{
player = GetComponent<Player>();
backTime = GameObject.FindGameObjectsWithTag("back");
gameManager = GameObject.FindGameObjectWithTag("Game Manager").GetComponent<GameManager>();
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
backTimeButton = Input.GetButton("Back");
stopTimeButton = Input.GetButton("Stop Time");
jumpButton = Input.GetButtonDown("Jump");
pickUpButton = Input.GetButtonDown("PickUp");
runButton = Input.GetButton("Run");
pauseButton = Input.GetButtonDown("Pause");
cancel = Input.GetButtonDown("Cancel");
if (!GameManager.pause)
{
player.PickupObject(pickUpButton);
player.Jump(jumpButton);
}
gameManager.Pause(pauseButton, cancel);
}
private void FixedUpdate()
{
if (!GameManager.pause)
{
player.Movement(horizontal, vertical, runButton);
player.StopTime(stopTimeButton, backTimeButton);
for (int i = 0; i < backTime.Length; i++)
{
backTime[i].GetComponent<BackTime>().Back(backTimeButton, stopTimeButton);
}
}
}
private void LateUpdate()
{
if (!GameManager.pause)
{
player.FpsMouse(mouseX, mouseY, sensyX, sensyY);
}
}
}
- Código:
using UnityEngine;
using Cinemachine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(AudioSource))]
public class Player : MonoBehaviour
{
private const float normalize = 0.01666769f;
public AudioClip footStepConcrete;
[HideInInspector]
public bool stopTimeAux;
public LayerMask layerMask;
[HideInInspector]
public GameObject objectDestroyer;
[HideInInspector]
public bool PickUp;
public float speedFov = 1;
public float fovMin = 75f;
public float fovMax = 105f;
public float speedFootStepWalk=1;
public float speedFootStepRun = 0.5f;
public float jumpForce = 100;
public float distance = 0.5f;
public float distancePickupObject = 5f;
public float timeStop = 5f;
public float speedPlayerWalk;
public float speedPlayerRun = 16f;
[HideInInspector]
public float time;
public float minY = -45;
public float maxY = 45;
public bool running;
private float mouseX;
private float mouseY;
private float timeFootStep;
public bool isGround;
private AudioSource audioSource;
private GameObject _camera;
private GameObject raycastPosition;
private GameObject posObject;
private CinemachineVirtualCamera baseCamera;
private Rigidbody rb;
private RaycastHit hit;
void Awake()
{
posObject = GameObject.Find("Pos Object");
_camera = GameObject.FindGameObjectWithTag("MainCamera");
raycastPosition = GameObject.Find("RayCast");
rb = GetComponent<Rigidbody>();
audioSource = GetComponent<AudioSource>();
baseCamera = GameObject.Find("VirtualCamera").GetComponent<CinemachineVirtualCamera>();
}
private void Update()
{
isGround = Physics.Raycast(raycastPosition.transform.position, -transform.up, distance);
}
public void StopTime(bool stopTimeButton, bool backTimeButton)
{
/*if (time < timeStop)
{*/
if (stopTimeButton)
{
//time += normalize;
stopTimeAux = true;
}
else
{
stopTimeAux = false;
}
//}
/*else
{
stopTimeAux = false;
}*/
}
public void PickupObject(bool PickUpButton)
{
bool objectPick = Physics.Raycast(_camera.transform.position, _camera.transform.forward, out hit, distancePickupObject, layerMask);
if (objectPick)
{
if (PickUpButton&&!PickUp)
PickUp = true;
else if (PickUpButton&&PickUp)
PickUp = false;
}
if (PickUp)
{
GameObject objectAux = null;
if (hit.rigidbody != null)
{
if (objectAux == null)
{
if (hit.collider.gameObject.tag == "PickUp")
{
objectAux = hit.collider.gameObject;
hit.transform.SetParent(posObject.transform);
hit.transform.position = posObject.transform.position;
hit.transform.rotation = posObject.transform.rotation;
hit.rigidbody.isKinematic = true;
objectDestroyer = objectAux;
}
}
}
}
else
{
if (hit.rigidbody != null)
{
hit.rigidbody.isKinematic = false;
hit.transform.SetParent(null);
}
}
}
public void Movement(float horizontal, float vertical,bool run)
{
FootStepSound(horizontal, vertical, run);
//ativar corrida
if (run && vertical > 0.2f)
{
running = true;
}
else if (vertical < 0.7f)
{
running = false;
}
else if (vertical == 0.0f)
{
running = false;
}
#region Running
//jogador correndo
if (running)
{
transform.Translate(horizontal * normalize * speedPlayerRun, 0, vertical * normalize * speedPlayerRun);
baseCamera.m_Lens.FieldOfView += normalize*speedFov;
if (baseCamera.m_Lens.FieldOfView >= fovMax)
{
baseCamera.m_Lens.FieldOfView = fovMax;
}
}
//jogador andando
else
{
transform.Translate(horizontal * normalize * speedPlayerWalk, 0, vertical * normalize * speedPlayerWalk);
baseCamera.m_Lens.FieldOfView -= normalize*speedFov*2;
if (baseCamera.m_Lens.FieldOfView <= fovMin)
{
baseCamera.m_Lens.FieldOfView = fovMin;
}
}
#endregion
}
void FootStepSound(float horizontal,float vertical,bool run)
{
//verifica se está no solo
if (isGround)
{
//andando pra frente e para trás toca o som de passos
if (vertical >= 0.4f || horizontal >= 0.4f)
timeFootStep += normalize;
else if (vertical <= -0.4f || horizontal <= -0.4f)
timeFootStep += normalize;
else if (vertical == 0 || horizontal == 0.0f)
timeFootStep = 0;
//toca o som do jogador andando
if (!running)
{
if (timeFootStep >= speedFootStepWalk)
{
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(footStepConcrete);
timeFootStep = 0;
}
}
}
else
{
//toca o som do jogador correndo
if (timeFootStep >= speedFootStepRun)
{
if (!audioSource.isPlaying)
{
audioSource.PlayOneShot(footStepConcrete);
timeFootStep = 0;
}
}
}
}
}
public void Jump(bool jump)
{
//bool canJump = true;
if (isGround)
if (jump)
{
rb.AddForce(transform.up * jumpForce * 2 * normalize, ForceMode.Impulse);
}
}
public void FpsMouse(float mouseX, float mouseY, float sensyX, float sensyY)
{
this.mouseX = mouseX * sensyX * Time.deltaTime;
this.mouseY += mouseY * sensyY * Time.deltaTime;
this.mouseY = Mathf.Clamp(this.mouseY, minY, maxY);
_camera.transform.localEulerAngles = new Vector3(-this.mouseY, 0, 0);
transform.Rotate(0, this.mouseX, 0);
}
}
diegopds- Mestre
- PONTOS : 3420
REPUTAÇÃO : 52
Idade : 26
Respeito as regras :
Tópicos semelhantes
» [Dúvida] Otimização
» Converter um script de otimização 3D para 2D
» Duvida sobre otimização e scripts.
» Coloca 2 script juntos iria ferra a otimização ?
» DÚVIDA - Script
» Converter um script de otimização 3D para 2D
» Duvida sobre otimização e scripts.
» Coloca 2 script juntos iria ferra a otimização ?
» DÚVIDA - Script
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos