- Home /
Joystick & RotateAround
Hey, so basicly i am making a game and im trying to make it for android. My player is rotating around Vector3.zero which works perfect with PC. But i dont know how i can translate the code to work with the Joystick. I have tried stuff and i only got horizontal to work with just changing movement
to Joystick.horizontal
but i want that the player is as the position as the joystick when its e.g up left so the player is up left Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float moveSpeed = 500f;
public Joystick joystick;
float movement = 0f;
void Update()
{
movement = Input.GetAxisRaw("Horizontal");
}
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
}
}
Answer by ArminAhmadi · Jan 28, 2019 at 05:13 AM
I would recommend you to use unity standard assets. First import the unity cross platform input :
Right click on assets folder in your project view.
Go to "Import Package" and click on "CrossPlatformInput".
After you imported cross platform input, Go to this path "Standard Assets/CrossPlatformInput/Scripts" and create your script there which I think is Player as you posted in your question above. This should be your new script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour
{
public float moveSpeed = 10;
private float hMovement = 0f;
private float vMovement = 0f;
private void Update()
{
// movement = Input.GetAxisRaw("Horizontal");
hMovement = CrossPlatformInputManager.GetAxisRaw("Horizontal");
vMovement = CrossPlatformInputManager.GetAxisRaw("Vertical");
}
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, hMovement * Time.fixedDeltaTime * -moveSpeed);
transform.RotateAround(Vector3.zero, Vector3.right, vMovement * Time.fixedDeltaTime * -moveSpeed);
}
}
Now go to this path "Standard Assets/CrossPlatformInput/Prefabs" and find "MobileSingleStickControl" then drag it in your scene. Attach the player script to your player game object (whatever that is). You are good to go. It would be better to place an event system in your scene but it usually will be created automatically on play.