how can i do rotation with a joystick

i have done code using a prefab joystick system however i can’t figure out how to get it to rotate. i am using this pack Joystick Pack | Input Management | Unity Asset Store. here is the code i am using for my movement/rotation. it is very messy.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpforce;
private float moveinput;
private Vector2 rotation;

public Joystick joystick;

private Rigidbody2D rb;

private bool facingright = true;

private bool isgrounded;
public Transform groundcheck;
public float checkradius;
public LayerMask whatisground;

private int extrajumps;
public int extjump;

void Start()
{
rb = GetComponent();
extrajumps = extjump;

}

void FixedUpdate()
{
isgrounded = Physics2D.OverlapCircle(groundcheck.position, checkradius, whatisground);

moveinput = joystick.Horizontal;
rotation = joystick.Direction;
rb.velocity = new Vector2(moveinput * speed, rb.velocity.y);
rb.rotation = rotation;

}

void Update()
{

float verticalmove = joystick.Vertical;

if (verticalmove >= .5f && extrajumps > 0)
{

rb.velocity = Vector2.up * jumpforce;
extrajumps–;
} else if(Input.GetKeyDown(KeyCode.W) && extrajumps == 0 && isgrounded == true)
{
rb.velocity = Vector2.up * jumpforce;
}

if (isgrounded == true)
{
extrajumps = extjump;
}
}

void Flip()
{

facingright = !facingright;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;

}

public void TakeDamage()
{
SceneManager.LoadScene(“DED”);
}

private void OnTriggerEnter2D(Collider2D collision)
{
spring hit = collision.GetComponent();
if (hit != null)
{

rb.velocity = rb.transform.up * 30;
}

}

}

Please use code tags .

My silver-bullet guess is that you’re trying to treat rigidbody.rotation the same way you treat rigidbody.velocity. The rotation doesn’t represent the rate of rotation, it represents the amount of rotation–the exact direction you’re currently facing.

The scripting API suggests this approach for making a rigidbody rotate smoothly over time:

    void FixedUpdate()
    {
        Quaternion deltaRotation = Quaternion.Euler(xRotateSpeed, yRotateSpeed, zRotateSpeed);
        m_Rigidbody.MoveRotation(m_Rigidbody.rotation * deltaRotation);
    }

But I don’t know anything about the specific asset you’re using, and I don’t know what “joystick.direction” conceptually means or what format it uses for its data. If you need help with a specific plugin, you usually will need to contact the plugin author, because finding someone on the scripting forum who’s familiar with the EXACT asset you’re using can be tricky.

You can also apply general debugging steps; for instance, Debug.Log(joystick.direction) and see what sort of numbers you’re getting (and whether you’re getting anything at all for the joystick you’re actually testing with!)

I always use a ‘container’ gameobject for anything I’m controlling, attach the script to the parent object, and generate the player gameobject at runtime.

But you can calculate the joystick values to rotation of the character using:

float angleA = Mathf.Atan2(xbox_rsx, xbox_rsy) * Mathf.Rad2Deg;

xbox_rsx/xbox_rxy are the right stick values (x is 4th axis and y is 5th axis on xb360 controller), and then use:

transform.rotation = Quaternion.Euler(0f, angleA, 0f);

on your player.

3 Likes

Sorry for late Reply but this worked in my case:
This should work on 2D games for 3D you just have to change the variabes for X or Y

        if (Input.touchCount > 0)
        {
            if (Mathf.Atan2(-joystick.Horizontal, joystick.Vertical) * Mathf.Rad2Deg != 0)
            { angleA = Mathf.Atan2(-joystick.Horizontal, joystick.Vertical) * Mathf.Rad2Deg; }
            transform.eulerAngles = new Vector3(0f, 0f, angleA + 90);
            print("changing direction to" + (angleA + 90));
        }
1 Like