Как управлять по-разному двумя разными джойстиками

Программирование для iPhone & Android

Как управлять по-разному двумя разными джойстиками

Сообщение Artuchka 08 сен 2017, 18:59

Как управлять по-разному двумя разными джойстиками(на Canvas) в 3D Unity игре?


Как присвоить разные функции двум разным джойстикам? И как определить, что это они именно были нажаты. Я попытался перерыть матриал по Touch, fingerId, GetComponent,GetTouch и т.д. + так же смотрел инфу по присвоению id объекту, но мне это не особо помогло, или я просто не смог правильно возпользоваться этой всей информацией. Так или иначе можете помочь, объяснить или дать подсказку, или дать код(может хотя бы кусочек)

Вот стате содержимое моих файлов со скриптами: PlayerController.cs:
using UnityEngine;
using CnControls;
using System;

[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour {

[SerializeField]
private float speed = 5f;

[SerializeField]
private float lookSpeed = 3f;

private PlayerMotor motor;

private void Start()
{
motor = GetComponent <PlayerMotor> ();
}



private void Update()
{

float xMov = CnInputManager.GetAxisRaw("Horizontal");
float zMov = CnInputManager.GetAxisRaw("Vertical");
//Input.GetAxisRaw("Vertical");

Vector3 movHor = transform.right * xMov;
Vector3 movVer = transform.forward * zMov;

Vector3 velocity = (movHor + movVer).normalized * speed;

motor.Move(velocity);

// float yRot = Input.GetAxisRaw("Mouse X");
//Vector3 rotation = new Vector3(0f, yRot, 0f) * lookSpeed;

float yRot = CnInputManager.GetAxisRaw(axisName: "Joystick 2");
Vector3 rotation = new Vector3(0f, yRot, 0f) * lookSpeed;
motor.Rotate(rotation);

// float xRot = Input.GetAxisRaw("Mouse Y");
// Vector3 camRotation = new Vector3(xRot, 0f, 0f) * lookSpeed;
float xRot = CnInputManager.GetAxisRaw(axisName: "Joystick 2");
Vector3 camRotation = new Vector3(xRot, 0f, 0f) * lookSpeed;

motor.RotateCam(camRotation);
}
}
PlayerMotor.cs:

using UnityEngine;
using CnControls;

[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour {
[SerializeField]
private Camera cam;

private Rigidbody rb;
private Vector3 velocity = Vector3.zero;
private Vector3 rotation = Vector3.zero;
private Vector3 camRotation = Vector3.zero;

private void Start()
{
rb = GetComponent <Rigidbody> ();
}

public void Move(Vector3 _velocity)
{
velocity = _velocity;
}

public void Rotate(Vector3 _rotaton)
{
rotation = _rotaton;
}

public void RotateCam(Vector3 _camRotaton)
{
camRotation = _camRotaton;
}
void FixedUpdate()
{
PerformMove();
PerformRotation();
}

void PerformMove()
{
if (velocity != Vector3.zero)
rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
}

void PerformRotation()
{
rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));
if (cam != null)
cam.transform.Rotate(-camRotation);
}
}
Стате, компоненты(два джойстика и кнопка) из asset store я использую - CnControls

Очень благодарен заранее.

P.S. Я - глубокий новичок в Unity и С#, так пожалуйста, сильно не злитесь, если вопрос глупый) Ах и стате, игра - 3D "шутер", если её так можно назвать)
+ Закомментированный код в PlayerController.cs - это управление передвижением игрока с помощью мышки и клавиатуры.
А код после закоментированного является как раз моей попыткой сделать управление с помощью двух джойстиков
Artuchka
UNец
 
Сообщения: 1
Зарегистрирован: 08 сен 2017, 18:47

Re: Как управлять по-разному двумя разными джойстиками

Сообщение TheSimplest 18 дек 2017, 04:09

Может это видео поможет? https://www.youtube.com/watch?v=6J6-7MV6nrA&t=1s По аналогии я сделал и второй стик
TheSimplest
UNец
 
Сообщения: 13
Зарегистрирован: 18 дек 2017, 04:01


Вернуться в iPhone & Android

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 4