Hi StackOverflow community, I have one common issue and need your help. I am a newbie in Unity but trying each day to improve my knowledge, sooo... Recently, I completed one of the Udemy courses and now trying to add new features and run the game on a Mobile device. I added a Virtual joystick asset and already changed movement with that Joystick. But for aim functionality, it is hard for me to change to Joystick.
What I changed already:
//moveInput.x = Input.GetAxisRaw("Horizontal"); // PC
//moveInput.y = Input.GetAxisRaw("Vertical"); // PC
moveInput.x = movingJoystick.Horizontal; // Mobile
moveInput.y = movingJoystick.Vertical; // Mobile
But now I need to change Vector3 mousePos = Input.mousePosition; to something similar to weapon.Joystic.Horizonral as I did with movement.
Please, advise some good implementations.
Here is a part of the code that handles character's and weapon rotation:
if (canMove && !LevelManager.instance.isPaused)
{
//moveInput.x = Input.GetAxisRaw("Horizontal"); // PC
//moveInput.y = Input.GetAxisRaw("Vertical"); // PC
moveInput.x = movingJoystick.Horizontal; // Mobile
moveInput.y = movingJoystick.Vertical; // Mobile
moveInput.Normalize();
//transform.position += new Vector3(moveInput.x * Time.deltaTime * moveSpeed, moveInput.y * Time.deltaTime * moveSpeed, 0f);
theRB.velocity = moveInput * activeMoveSpeed;
Vector3 mousePos = Input.mousePosition;
Vector3 screenPoint = CameraController.instance.mainCamera.WorldToScreenPoint(transform.localPosition);
if (mousePos.x < screenPoint.x)
{
transform.localScale = new Vector3(-1f, 1f, 1f);
gunArm.localScale = new Vector3(-1f, -1f, 1f);
}
else
{
transform.localScale = Vector3.one;
gunArm.localScale = Vector3.one;
}
//rotate gun arm
Vector2 offset = new Vector2(mousePos.x - screenPoint.x, mousePos.y - screenPoint.y);
float angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
gunArm.rotation = Quaternion.Euler(0, 0, angle);
}