- Home /
Dual virtual Joystick move and shoot
Trying to make a dual virtual Joystick in C# to work for a 3D Mobile game. I'm not using any import packages, cause the requirements seem to have changed a couple times over the various releases of Unity. I have the left virtual joystick moving the player working. I cannot seem to get the right virtual joystick to aim the weapon.
I'm using canvas and images to create the joystick art. The 3D weapon in attached to the 3D player using "child.transform.position = parent.transform.position"
While using the right virtual joystick, I'm trying to get the weapon to clamp the rotate + and - 89.9 degrees on the X axis. Also clamp the Y axis to + and - 89.9 degrees. No Z axis rotation.
Does anyone know of a tutorial that will work for Unity 5.2.2? Previous youtube tutorials were using older versions of Unity that do not seem to be supported anymore in code. The Devon Curry tutorials were really good, but they work for 4.6 3D scenes, and the assets store imports he uses are not available anymore. His new tutorials for 5.2 are for 2D games.
Do I have to use the crossplatform code?
Thank you for your time,
This is the code I'm using for the working left (move) virtual joystick. Hopefully this will help anyone understand what I'm currently doing.
[Code]
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems;
public class VirtualJoystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler { public enum JoystickType {Movement, LookRotation, Jump}; public JoystickType joystickType; private Image bgImg; private Image joystickImg; private Vector3 inputVector;
private void Start()
{
bgImg = GetComponent<Image>();
joystickImg = transform.GetChild (0).GetComponent <Image>();
}
public virtual void OnDrag(PointerEventData ped)
{
Vector2 pos;
if(RectTransformUtility.ScreenPointToLocalPointInRectangle (bgImg.rectTransform,ped.position, ped.pressEventCamera, out pos ))
{
pos.x = (pos.x / bgImg.rectTransform.sizeDelta.x);
pos.y = (pos.y / bgImg.rectTransform.sizeDelta.y);
inputVector = new Vector3(pos.x*2 + 1, 0, pos.y*2 - 1);
inputVector = (inputVector.magnitude > 1.0f)?inputVector.normalized:inputVector;
Debug.Log ("I can touch the joystick image");
// Move Joystick IMG
joystickImg.rectTransform.anchoredPosition = new Vector3(inputVector.x * (bgImg.rectTransform.sizeDelta.x/3), inputVector.z * (bgImg.rectTransform.sizeDelta.y/3));
}
}
public virtual void OnPointerDown(PointerEventData ped)
{
}
public virtual void OnPointerUp(PointerEventData ped)
{
// return joystick to center after being moved
inputVector = Vector3.zero;
joystickImg.rectTransform.anchoredPosition = Vector3.zero;
}
public float Horizontal()
{
if (inputVector.x != 0 )
return inputVector.x;
else
return Input.GetAxis ("Horizontal");
}
public float Vertical ()
{
if (inputVector.z != 0 )
return inputVector.z;
else
return Input.GetAxis ("Vertical");
}
} [/Code]
Sean
This isn't exactly an answer to your question, but it might help. When working with new controllers and such, I always create a test project that displays to screen the name of the button that i'm pressing. To do this, build an empty screen with a UI text object on it. Then listen for every potential input from the controller and when you get one, just update the text object to show it's name.
It could be something as simple as correctly identifying the name of the input for the thumbstick you are trying to work with.
Answer by seanrod · Mar 03, 2016 at 06:47 PM
I got it to work by using the Unity SingleStickControlControl for the left stick that will move the player. I placed the SingleStickControlControl prefab on the left side of the screen. To get the left stick to work I used the Devon Curry video I posted up above.
For the Right Stick I used a free asset from the Unity Store: https://www.assetstore.unity3d.com/en/#!/content/15233
After downloading the package, I followed the simple instructions for the CN Controls: Created a canvas and dragged in the Cn joystick. Dragged the joystick to the right side of the screen to be the right joystick. I applied the given CN edited script "ThidPersonExampleController" to my cannon. Then on the cannon, uncheck the "CharacterController" in the Inspector. To keep the cannon attached to the player, I added a script to make the cannon a child of the player at all times.
This is the script that attaches the cannon to the player: public GameObject child; public GameObject parent;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
child.transform.position = parent.transform.position;
}
Then. press play.
Thank you SOOOOooooo Much, Devon Curry and CN CONTROLS!!!
Sean
Answer by Dimwood2011 · Feb 27, 2016 at 07:56 AM
use MouseLook script for the right analog stick to aim. Create your own script called MouseX2 You will have to create a MouseX2 input name called MouseX2, and im pretty sure MouseY2 .just look at the MouseLook script to see if it has MouseX and MouseY.Once setting up inputs you should be able to use your right analog just like your mouse.I think joystick axis is 4th and 5th axis for Xbox.Maybe some one can help with the converting to mobile button mapping from there.
Answer by seanrod · Feb 29, 2016 at 06:44 PM
Thank you Magius96 and Dimwood2011. I have a test scene that print out Debug logs to tell me what if the script is working. The controller is working, but I just found out that the best way to get the dual virtual joysticks to work with mobile devices is to use the "MobileSingleStickControl."
I can bring in the 3rd person standard asset, , EventSystem and the "MobileSingleStickControl." I can see the player move around.
But, when I bring in the "MobileSingleStickControl" , EventSystem, and my player I do not see the player move. My player is using the "Horizontal" and "Vertical" inputs. I can use WASD to control the player. But for some reason, when I use the "MobileSingleStickControl" to move my player, nothing happens.
I thought the "MobileSingleStickControl" hooked into the "Horizontal" and "Vertical" inputs and would just work, but that does not seem to be the case. Need to figure out how to hook up the MobileSingleStickControl" to my character. Then maybe, I can hook up the right joystick to the MouseX2 like Dimwood2011 suggested.
I'm trying to get a simple ball to move around with the Mobile Joystick. This is the code I'm using for myball. public float moveSpeed = 5.0f; public float drag = 0.5f; public float terminalRotationSpeed = 25.0f; public Vector3 MoveVector{set; get;} //public VirtualJoystick joystick;
private Rigidbody thisRigidbody;
private void Start()
{
thisRigidbody = gameObject.AddComponent<Rigidbody>();
thisRigidbody.maxAngularVelocity = terminalRotationSpeed ;
thisRigidbody.drag = drag;
}
// Update is called once per frame
void Update ()
{
MoveVector = PoolInput();
Move();
}
private void Move ()
{
thisRigidbody.AddForce((MoveVector * moveSpeed));
}
private Vector3 PoolInput()
{
Vector3 dir = Vector3.zero;
// commented out to hook up to virtual joystick
dir.x = Input.GetAxis ("Horizontal");
dir.z = Input.GetAxis ("Vertical");
//dir.x = joystick. Horizontal();
//dir.z = joystick. Vertical();
if(dir.magnitude > 1)
dir.Normalize ();
return dir;
Sean
Devon Curry's tutorial is fantastic!!!! https://www.youtube.com/watch?v=nGYObojmkO4
In the comments, Chaoticdanor wrote a way to change the 2d motor to a 3D motor.
"Great video, for anyone who'd like to use this on 3d rigidbodies(and colliders), you'll need to change the code. replace this: Rigidbody2D myBody; Vector2 moveVec = new Vector2(CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal"), CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical")) moveForce; bool isBoosting = CrossPlatformInput$$anonymous$$anager.GetButton("Boost"); myBody.AddForce(moveVec); by public Rigidbody myBody; Vector3 moveVec = new Vector3(CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal"),0, CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical")) moveForce; bool isBoosting = CrossPlatformInput$$anonymous$$anager.GetButton("Boost"); myBody.AddForce(moveVec); As you can see i've put a 0 in one of the 3 axis, so it'll still behave the same as the 2d version but if your setup is different than $$anonymous$$e you may need to switch the 0 with another axis. For example: new Vector3(CrossPlatformInput$$anonymous$$anager.GetAxis("Horizontal"), CrossPlatformInput$$anonymous$$anager.GetAxis("Vertical"),0)"
Anyone know how to get the right $$anonymous$$obileSingleStickControl to rotate a weapon?
Sean