2D arm rotation with mobile controls

Started by
2 comments, last by Alberth 7 years, 2 months ago
Hello,

I have been working on a 2D platformer and right now I have set up in a way that makes the arm follow the mouse cursor.
I want to be able to have it so that when you move the joystick it moves the arm as well.

Here is the arm rotation script

using UnityEngine;
using System.Collections;
public class ArmRotation : MonoBehaviour {
public int rotationOffset = 0;
void Update () {
Vector3 mousePos = Input.mousePosition;
mousePos.z = 5.23f;
Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
mousePos.x = mousePos.x – objectPos.x;
mousePos.y = mousePos.y – objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
}
Advertisement

In the technical sense, I see no problem, I mean, just get the joystick input, and add it to the computation, or am I missing something?

What you may want to consider first is what should happen if you switch controls during movement.

In the technical sense, I see no problem, I mean, just get the joystick input, and add it to the computation, or am I missing something?

What you may want to consider first is what should happen if you switch controls during movement.

Thanks for the respose I did get it to work but now I ran into another issue and I am wodering if you can help me?

The arm now rotates but the bullet trail that comes out of the gun to kill the enemies dosent follow the joystick.

I have tried to change the Input to CrossPlatformInputManger but all it does is stay in one place

here is the code that shoots

void Shoot ()
{
Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
Debug.DrawLine(firePointPosition, (mousePosition - firePointPosition)*100, Color.cyan);
if (hit.collider != null)
{
Debug.DrawLine(firePointPosition, hit.point, Color.red);
Enemy enemy = hit.collider.GetComponent<Enemy>();
if (enemy != null)
{
enemy.DamageEnemy(Damage);
}
}

Your Unity experience is much larger than mine, as mine is exactly 0.

Having said that, I see something called "mousePosition", which seems to use "Input.mousePosition". No idea why you compute Camera.main.ScreenToWorldPoint() twice on the same input, but it looks like it should functionally work.

In particular, what I don't see here is anything joystick related, how do you expect this code to react on the joy stick if you don't tell it how to use its coordinates?

This topic is closed to new replies.

Advertisement