I'm just starting to learn Unity and I must admit at the moment im struggling linking my code scripts together. Im used to having a main class to work from.
I have been trying to implement virtual touch joysticks. I have the following code and it does control the player, but it is just moving a rigidbody.
What I want is to make this virtual joystick give out a relevant X,Y vector2 so that I can access it in another class, specifically for this case I am trying to use it with the prefab Car from the Standard Assets pack. All I am trying to do for now is if up is held, accelerate the car.
The car prefab code is not altered so far. And therefore it works if I use keyboard, but if I build for Android the touch joysticks respond to touch but I cant get the car to move.
Here is the joystick code im using:
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;
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x - deltaValues.y * Time.deltaTime * speedProgressiveLook,
transform.localEulerAngles.y + deltaValues.x * Time.deltaTime * speedProgressiveLook,
0f);
}
}
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)
{
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x - rightController.GetTouchPosition.y * Time.deltaTime * speedContinuousLook,
transform.localEulerAngles.y + rightController.GetTouchPosition.x * Time.deltaTime * speedContinuousLook,
0f);
}
}
void OnDestroy()
{
rightController.TouchEvent -= RightController_TouchEvent;
rightController.TouchStateEvent -= RightController_TouchStateEvent;
}
}
Again, the question is how do I get a public variable Vector2 of the X and Y movement of the joystick, instead of directly moving the rigid body from inside this code.
Thanks for any help.
EDIT: The rigidbody is just in a primitive sphere for my testing purpose, it has nothing to do with the car prefab. I want to change the sphere for a car prefab and using the XY Vector I was talking about above alter the car controller code to take those values and accelerate, brake etc.
public Vector2field/property/getter method and providing a reference to this input script to the gameplay script that needs to query it. Classic methods are assigning the reference through an Inspector variable, passing it to an initialization method when spawning the player, finding it through a search using eg.FindObjectOfTypeor exposing it as a static getter or singleton. What have you tried so far? Do you need specific help getting any of those options to work? \$\endgroup\$