Movement with joystick ,Moving object with the Joystck
Hi guys i have been working in a mobile game i have two different scripts one for movement and the ther one for the joystick the problem is that i need a way to connect them to make the player move around with the joystick please help me its been a week and i don't know what to do thanks
movement script :
public float m_Speed = 12f; // How fast the tank moves forward and back. public float m_TurnSpeed = 180f; // How fast the tank turns in degrees per second. public VirtualJoystick joystick;
private string m_MovementAxisName; // The name of the input axis for moving forward and back.
private string m_TurnAxisName; // The name of the input axis for turning.
private Rigidbody m_Rigidbody; // Reference used to move the tank.
private float m_MovementInputValue; // The current value of the movement input.
private float m_TurnInputValue; // The current value of the turn input.
private void Awake()
{
m_Rigidbody = GetComponent<Rigidbody>();
}
private void OnEnable()
{
// When the tank is turned on, make sure it's not kinematic.
m_Rigidbody.isKinematic = true;
// Also reset the input values.
m_MovementInputValue = 0f;
m_TurnInputValue = 0f;
}
private void OnDisable()
{
// When the tank is turned off, set it to kinematic so it stops moving.
m_Rigidbody.isKinematic = true;
}
private void Start()
{
// The axes names are based on player number.
m_MovementAxisName = "Vertical";
m_TurnAxisName = "Horizontal";
}
private void Update()
{
// Store the value of both input axes.
m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
m_TurnInputValue = Input.GetAxis(m_TurnAxisName);
}
private void FixedUpdate()
{
// Adjust the rigidbodies position and orientation in FixedUpdate.
Move();
Turn();
}
private void Move()
{
// Create a vector in the direction the tank is facing with a magnitude based on the input, speed and the time between frames.
Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;
// Apply this movement to the rigidbody's position.
m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
}
private void Turn()
{
// Determine the number of degrees to be turned based on the input, speed and time between frames.
float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;
// Make this into a rotation in the y axis.
Quaternion turnRotation = Quaternion.Euler(0f, turn, 0f);
// Apply this rotation to the rigidbody's rotation.
m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
}
}
joystick script :
private Image bgImg; private Image joystickImg; private Vector3 inputVector;
private void Start()
{
bgImg = GetComponent<Image>();
joystickImg = transform.GetChild(0).GetComponent<Image>();
}
//EventSystems interfaces
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle(bgImg.rectTransform,ped.position,ped.pressEventCamera,out pos))
{
pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x);
pos.y = (pos.y / bgImg.rectTransform.sizeDelta.y);
inputVector = new Vector3(pos.x * 2 + 1, 0, pos.y * 2 - 1);
inputVector = (inputVector.magnitude > 1.0f) ? inputVector.normalized : inputVector;
joystickImg.rectTransform.anchoredPosition = new Vector3 (inputVector.x*(bgImg.rectTransform.sizeDelta.x / 3),inputVector.z*(bgImg.rectTransform.sizeDelta.y/3));
}
}
public virtual void OnPointerDown(PointerEventData ped)
{
OnDrag(ped);
}
public virtual void OnPointerUp(PointerEventData ped)
{
inputVector = Vector3.zero;
joystickImg.rectTransform.anchoredPosition = Vector3.zero;
}
public float Horizontal()
{
if (inputVector.x != 0)
return inputVector.x;
else
return Input.GetAxis("Horizontal");
}
public float Vertical()
{
if (inputVector.x != 0)
return inputVector.z;
else
return Input.GetAxis("Vertical");
}
}
Have you setup Joystick in Input $$anonymous$$anager?
If not, just add new "Horizontal" and "Vertical" with input from joystick axis
Your answer
Follow this Question
Related Questions
WASD Joystick Movement Android 0 Answers
Player i spining when moving 0 Answers
My character carries on the run animation for a few seconds even after standing still 0 Answers
Character movement 0 Answers
Make character face movement direction 0 Answers