- Home /
Joystick problem (i only want horizontal movement)
Hey all, i have 2 joysticks, one for movement and one for rotation, but i want the rotation one to only rotate left and right and not with the Z axis.
On the code below if i have that part commented in the end it works 100% as intended on computer but breaks on mobile,
any help pls?
i have the following code:
using UnityEngine; using System.Collections;
[RequireComponent(typeof(Rigidbody))]
public class PlayerMoveController : MonoBehaviour {
// PUBLIC
public SimpleTouchController leftController;
public SimpleTouchController rightController;
public float speedMovements = 5f;
public float speedContinuousLook = 100f;
public float speedProgressiveLook = 3000f;
public bool continuousRightController = true;
// PRIVATE
private Rigidbody _rigidbody;
private Vector3 localEulRot;
private Vector2 prevRightTouchPos;
void Awake()
{
_rigidbody = GetComponent<Rigidbody>();
rightController.TouchEvent += RightController_TouchEvent;
rightController.TouchStateEvent += RightController_TouchStateEvent;
}
public bool ContinuousRightController
{
set{continuousRightController = value;}
}
void RightController_TouchStateEvent (bool touchPresent)
{
if(!continuousRightController)
{
prevRightTouchPos = Vector2.zero;
}
}
void RightController_TouchEvent (Vector2 value)
{
if(!continuousRightController)
{
Vector2 deltaValues = value - prevRightTouchPos;
prevRightTouchPos = value;
Quaternion rot = Quaternion.Euler(transform.localEulerAngles.x /*- rightController.GetTouchPosition.y*/ * Time.deltaTime * speedContinuousLook,
transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook,
0f);
_rigidbody.MoveRotation(rot);
}
}
void Update()
{
// move
_rigidbody.MovePosition(transform.position + (transform.forward * leftController.GetTouchPosition.y * Time.deltaTime * speedMovements) +
(transform.right * leftController.GetTouchPosition.x * Time.deltaTime * speedMovements) );
if (continuousRightController)
{
Quaternion rot = Quaternion.Euler(transform.localEulerAngles.x /*- rightController.GetTouchPosition.y*/ * Time.deltaTime * speedContinuousLook,
transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook,
0f);
_rigidbody.MoveRotation(rot);
}
}
void OnDestroy()
{
rightController.TouchEvent -= RightController_TouchEvent;
rightController.TouchStateEvent -= RightController_TouchStateEvent;
}
}
Comment