[TUTORIAL] Script para jogo FPS Mobile
Página 1 de 1
[TUTORIAL] Script para jogo FPS Mobile
com o mouse ou o touch do celular, esse script movimenta a camera igual fps, também movimenta o personagem com W, A, S, D. para movimentar o personagem pelo celular vc usa o plugin da assetStore "SimpleInput" aí vc joga o prefab "Joystick" no canvas e ta pronto o fps mobile.
- Código:
[Header("Character Info")]
public CharacterController characterController;
public float movementSpeed;
public static bool isControllingCharacter;
float gravity = 9f;
Vector3 velocity;
[Header("Camera Info")]
public Transform characterCamera;
public float cameraSensitivity;
public static bool isControllingCamera;
float rotationY = 0f;
//Touch
float moveInputDeadZone;
float cameraPitch;
Vector2 lookInput;
int leftFingerId, rightFingerId;
float halfScreenWidth;
void Start()
{
isControllingCharacter = true;
isControllingCamera = true;
leftFingerId = -1;
rightFingerId = -1;
halfScreenWidth = Screen.width / 2.2f;
moveInputDeadZone = Mathf.Pow(Screen.height / moveInputDeadZone, 2);
}
void LateUpdate()
{
CharacterControlling();
CameraControlling();
GetTouchInput();
if (rightFingerId != -1)
{
LookAround();
}
}
void CharacterControlling()
{
if(isControllingCharacter == true)
{
float moveX = SimpleInput.GetAxis("CharacterHorizontal");
float moveZ = SimpleInput.GetAxis("CharacterVertical");
Vector3 move = transform.right * moveX + transform.forward * moveZ;
characterController.Move(move * movementSpeed * Time.deltaTime);
characterController.Move(velocity * Time.deltaTime);
velocity.y += -gravity * Time.deltaTime;
}
}
void CameraControlling()
{
if(isControllingCamera == true)
{
Cursor.lockState = CursorLockMode.Locked;
float mouseX = Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
rotationY -= mouseY;
rotationY = Mathf.Clamp(rotationY, -85f, 85f);
characterCamera.localRotation = Quaternion.Euler(rotationY, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
}
void GetTouchInput()
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch t = Input.GetTouch(i);
switch (t.phase)
{
case TouchPhase.Began:
if (t.position.x < halfScreenWidth && leftFingerId == -1)
{
leftFingerId = t.fingerId;
}
else if (t.position.x > halfScreenWidth && rightFingerId == -1)
{
rightFingerId = t.fingerId;
}
break;
case TouchPhase.Ended:
case TouchPhase.Canceled:
if (t.fingerId == leftFingerId)
{
leftFingerId = -1;
}
else if (t.fingerId == rightFingerId)
{
rightFingerId = -1;
}
break;
case TouchPhase.Moved:
if (t.fingerId == rightFingerId)
{
lookInput = t.deltaPosition * cameraSensitivity * Time.deltaTime;
}
break;
case TouchPhase.Stationary:
if (t.fingerId == rightFingerId)
{
lookInput = Vector2.zero;
}
break;
}
}
}
void LookAround()
{
if (isControllingCamera == true)
{
cameraPitch = Mathf.Clamp(cameraPitch - lookInput.y, -90f, 90f);
characterCamera.localRotation = Quaternion.Euler(cameraPitch, 0, 0);
transform.Rotate(transform.up, lookInput.x);
}
}
}
JulioWinchester- MembroAvançado
- PONTOS : 2093
REPUTAÇÃO : 48
Idade : 20
Áreas de atuação : https://linktr.ee/juliopepe
Respeito as regras :
Re: [TUTORIAL] Script para jogo FPS Mobile
JulioWinchester escreveu:com o mouse ou o touch do celular, esse script movimenta a camera igual fps, também movimenta o personagem com W, A, S, D. para movimentar o personagem pelo celular vc usa o plugin da assetStore "SimpleInput" aí vc joga o prefab "Joystick" no canvas e ta pronto o fps mobile.
- Código:
[Header("Character Info")]
public CharacterController characterController;
public float movementSpeed;
public static bool isControllingCharacter;
float gravity = 9f;
Vector3 velocity;
[Header("Camera Info")]
public Transform characterCamera;
public float cameraSensitivity;
public static bool isControllingCamera;
float rotationY = 0f;
//Touch
float moveInputDeadZone;
float cameraPitch;
Vector2 lookInput;
int leftFingerId, rightFingerId;
float halfScreenWidth;
void Start()
{
isControllingCharacter = true;
isControllingCamera = true;
leftFingerId = -1;
rightFingerId = -1;
halfScreenWidth = Screen.width / 2.2f;
moveInputDeadZone = Mathf.Pow(Screen.height / moveInputDeadZone, 2);
}
void LateUpdate()
{
CharacterControlling();
CameraControlling();
GetTouchInput();
if (rightFingerId != -1)
{
LookAround();
}
}
void CharacterControlling()
{
if(isControllingCharacter == true)
{
float moveX = SimpleInput.GetAxis("CharacterHorizontal");
float moveZ = SimpleInput.GetAxis("CharacterVertical");
Vector3 move = transform.right * moveX + transform.forward * moveZ;
characterController.Move(move * movementSpeed * Time.deltaTime);
characterController.Move(velocity * Time.deltaTime);
velocity.y += -gravity * Time.deltaTime;
}
}
void CameraControlling()
{
if(isControllingCamera == true)
{
Cursor.lockState = CursorLockMode.Locked;
float mouseX = Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
rotationY -= mouseY;
rotationY = Mathf.Clamp(rotationY, -85f, 85f);
characterCamera.localRotation = Quaternion.Euler(rotationY, 0f, 0f);
transform.Rotate(Vector3.up * mouseX);
}
}
void GetTouchInput()
{
for (int i = 0; i < Input.touchCount; i++)
{
Touch t = Input.GetTouch(i);
switch (t.phase)
{
case TouchPhase.Began:
if (t.position.x < halfScreenWidth && leftFingerId == -1)
{
leftFingerId = t.fingerId;
}
else if (t.position.x > halfScreenWidth && rightFingerId == -1)
{
rightFingerId = t.fingerId;
}
break;
case TouchPhase.Ended:
case TouchPhase.Canceled:
if (t.fingerId == leftFingerId)
{
leftFingerId = -1;
}
else if (t.fingerId == rightFingerId)
{
rightFingerId = -1;
}
break;
case TouchPhase.Moved:
if (t.fingerId == rightFingerId)
{
lookInput = t.deltaPosition * cameraSensitivity * Time.deltaTime;
}
break;
case TouchPhase.Stationary:
if (t.fingerId == rightFingerId)
{
lookInput = Vector2.zero;
}
break;
}
}
}
void LookAround()
{
if (isControllingCamera == true)
{
cameraPitch = Mathf.Clamp(cameraPitch - lookInput.y, -90f, 90f);
characterCamera.localRotation = Quaternion.Euler(cameraPitch, 0, 0);
transform.Rotate(transform.up, lookInput.x);
}
}
}
quando for compilar pra mobile tem q dar // no CameraControlling(); no late update
JulioWinchester- MembroAvançado
- PONTOS : 2093
REPUTAÇÃO : 48
Idade : 20
Áreas de atuação : https://linktr.ee/juliopepe
Respeito as regras :
Tópicos semelhantes
» [TUTORIAL] Script para medir o FPS do seu JOGO
» [TUTORIAL] Joystick virtual para mobile
» [Asset] Script de tiro para mobile
» [TUTORIAL] Criar Analógico Virtual UI Para Mobile.
» [TUTORIAL ] Script para Cutscenes
» [TUTORIAL] Joystick virtual para mobile
» [Asset] Script de tiro para mobile
» [TUTORIAL] Criar Analógico Virtual UI Para Mobile.
» [TUTORIAL ] Script para Cutscenes
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos