Jump to content
  • Advertisement
Sign in to follow this  
Cuber01

Unity 2D arm rotation with mobile controls

This topic is 669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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));
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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);
            }
        }

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!