Home>

unity and programming are both beginners.
Also, since this is the first time that a question has been asked through the site like this, there is not enough information to ask a question
I'd be happy if you could tell me anything that doesn't work.

To create a 3D game in Unity and make the platform a smartphone/tablet
I was thinking of using a joystick to move the character.
I'm in trouble because I can't relate the movement of the joystick and the character well.
The Unity version we are using is 2019.1.10f1 and we are running the SD character Unity-chan.

UnityStandardAssets>CrossPlatformInput>Created using the Joystick script in the Scripts folder
Position information from Joystick in UnityStandardAssets>ThirdPersonCharacter>Scripts folder
Newly created inputHorizontal in the ThirdPersonUserControl script, variable to replace Vertical
I'm trying to put it in but it doesn't work.

Script changes from the default state

After that, paste the corresponding script. Before you see it, change the default from
I wrote this because I thought it would be better to tell it earlier.
If it is not necessary because it is a premise to judge after reading the whole script, from the next `` corresponding source code '' column
I hope you can see it.

First ThirdPersonUseControl is the default 15th-16th line from the top

public float inputY;
public float inputX;


And added

on lines 51-54
// float h = CrossPlatformInputManager.GetAxis ("Horizontal");
// float v = CrossPlatformInputManager.GetAxis ("Vertical");(The two lines below are added for that purpose.)
float h = inputY;
float v = inputX;


Comment out 51,52
The point where 53 and 54 are newly added is the place where the change was made.

Next, Joystick changes
21st line from the top

public GameObject posController;// specify the object whose position is to be controlled


A one-line script is prepared to refer to a game object to which ThirdPersonUseControl is applied
Lines 120-121

posController.GetComponent<ThirdPersonUserControl>(). inputX = position.x;
posController.GetComponent<ThirdPersonUserControl>(). inputY = position.y;


Joystick position value to inputX, Y in ThirdPersonUseControl
The part that contains the two lines added for the purpose of delivery is the change.

Applicable source code ThirdPersonUseControl
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Characters.ThirdPerson
{
    [RequireComponent (typeof (ThirdPersonCharacter))]
    public class ThirdPersonUserControl: MonoBehaviour
    {
        private ThirdPersonCharacter m_Character;// A reference to the ThirdPersonCharacter on the object
        private Transform m_Cam;// A reference to the main camera in the scenes transform
        private Vector3 m_CamForward;// The current forward direction of the camera
        private Vector3 m_Move;
        private bool m_Jump;// the world-relative desired move direction, calculated from the camForward and user input.
        public float inputY;
        public float inputX;

        private void Start (){
            // get the transform of the main camera
            if (Camera.main! = null)
            {
                m_Cam = Camera.main.transform;
            }
            else
            {
                Debug.LogWarning (
                    "Warning: no main camera found. Third person character needs a Camera tagged \" MainCamera \ ", for camera-relative controls.", GameObject);
                // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
            }
            // get the third person character (this should never be null due to require component)
            m_Character = GetComponent<ThirdPersonCharacter>();
        }

        private void Update ()
        {
            if (! m_Jump)
            {
                m_Jump = CrossPlatformInputManager.GetButtonDown ("Jump");
            }
        }

        // Fixed update is called in sync with physics
        private void FixedUpdate ()
        {
            // read inputs
            // float h = CrossPlatformInputManager.GetAxis ("Horizontal");
            // float v = CrossPlatformInputManager.GetAxis ("Vertical");(The two lines below are added for that purpose.)
            float h = inputY;
            float v = inputX;
            bool crouch = Input.GetKey (KeyCode.C);
            // calculate move direction to pass to character
            if (m_Cam! = null)
            {
                // calculate camera relative direction to move:
                m_CamForward = Vector3.Scale (m_Cam.forward, new Vector3 (1, 0, 1)). normalized;
                m_Move = v * m_CamForward + h * m_Cam.right;
            }
            else
            {
                // we use world-relative directions in the case of no main camera
                m_Move = v * Vector3.forward + h * Vector3.right;
            }
#if! MOBILE_INPUT
            // walk speed multiplier
            if (Input.GetKey (KeyCode.LeftShift)) m_Move * = 0.5f;
#endif
            // pass all parameters to the character control script
            m_Character.Move (m_Move, crouch, m_Jump);
            m_Jump = false;
        }
    }
}
Joystick
using System;
using UnityEngine;
using UnityEngine.EventSystems;namespace UnityStandardAssets.CrossPlatformInput
{
    public class Joystick: MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
    {
        public enum AxisOption
        {
            // Options for which axes to use
            Both, // Use both
            OnlyHorizontal, // Only horizontal
            OnlyVertical // Only vertical
        }
        public int MovementRange = 100;
        public AxisOption axesToUse = AxisOption.Both;// The options for the axes that the still will use
        public string horizontalAxisName = "Horizontal";// The name given to the horizontal axis for the cross platform input
        public string verticalAxisName = "Vertical";// The name given to the vertical axis for the cross platform input
        public GameObject posController;// Specify the object whose position is to be controlled
        Vector3 m_StartPos;
        bool m_UseX;// Toggle for using the x axis
        bool m_UseY;// Toggle for using the Y axis
        CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis;// Reference to the joystick in the cross platform input
        CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis;// Reference to the joystick in the cross platform input
        void OnEnable ()
        {
            CreateVirtualAxes ();
        }
        void Start ()
        {
            m_StartPos = transform.position;
        }
        void UpdateVirtualAxes (Vector3 value)
        {
            var delta = m_StartPos-value;
            delta.y = -delta.y;
            delta/= MovementRange;
            if (m_UseX)
            {
                m_HorizontalVirtualAxis.Update (-delta.x);
            }
            if (m_UseY)
            {
                m_VerticalVirtualAxis.Update (delta.y);
            }
        }
        void CreateVirtualAxes ()
        {
            // set axes to use
            m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
            m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
            // create new axes based on axes to use
            if (m_UseX)
            {
                m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis (horizontalAxisName);
                CrossPlatformInputManager.RegisterVirtualAxis (m_HorizontalVirtualAxis);
            }
            if (m_UseY)
            {
                m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis (verticalAxisName);
                CrossPlatformInputManager.RegisterVirtualAxis (m_VerticalVirtualAxis);
            }}

        public void OnDrag (PointerEventData data)
        {
            Vector3 newPos = Vector3.zero;
            if (m_UseX)
            {
                int delta = (int) (data.position.x-m_StartPos.x);
                delta = Mathf.Clamp (delta,-MovementRange, MovementRange);
                newPos.x = delta;
            }
            if (m_UseY)
            {
                int delta = (int) (data.position.y-m_StartPos.y);
                delta = Mathf.Clamp (delta, -MovementRange, MovementRange);
                newPos.y = delta;
            }
            transform.position = new Vector3 (m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
            UpdateVirtualAxes (transform.position);
        }

        public void OnPointerUp (PointerEventData data)
        {
            transform.position = m_StartPos;
            UpdateVirtualAxes (m_StartPos);
        }

        public void OnPointerDown (PointerEventData data) {}
        void OnDisable ()
        {
            // remove the joysticks from the cross platform input
            if (m_UseX)
            {
                m_HorizontalVirtualAxis.Remove ();
            }
            if (m_UseY)
            {
                m_VerticalVirtualAxis.Remove ();
            }
        }
    }
    // float posController = GameObject.FindObjectOfType (ThirdPersonController) as ThirdPersonController;
    posController.GetComponent<ThirdPersonUserControl>(). inputX = position.x;
    posController.GetComponent<ThirdPersonUserControl>(). inputY = position.y;
}

I'm going to put here the URL of the site that I used as a reference.

Most visited sites
How to add Joystick in Unity [beginners]

Site based on how to refer to another object
Unity-touch other scripts

Other reference sites (reflection level: low)
How to move Unity with a joystick [smartphone compatible]
[Unity] How to reference scripts from other scripts

While looking at various sites (joysticks), I thought that the articles might use something different from the standard assets I use.
However, I don't know how to check what version of it I'm using, so I can't post it here.
I'm sorry.

  • Answer # 1

    I managed to self-solve (consult people).

    It seems that the person who had the problem was the Joystick script.
    The main reason was that I hadn't done that where I had to declareusing UnityStandardAssets.Characters.ThirdPerson.

    In addition, the following code written around the second line from the bottom of the original Joystick script is rewritten in the UpdateVirtualAxes class in the modified script.

    posController.GetComponent(). inputX = position.x; posController.GetComponent(). inputY = position.y;

    Revised source code Joystick
    using System;
    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityStandardAssets.Characters.ThirdPerson;
    namespace UnityStandardAssets.CrossPlatformInput
    {
        public class Joystick: MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
        {
            public enum AxisOption
            {
                // Options for which axes to use
                Both, // Use both
                OnlyHorizontal, // Only horizontal
                OnlyVertical // Only vertical
            }
            public int MovementRange = 100;
            public AxisOption axesToUse = AxisOption.Both;// The options for the axes that the still will use
            public string horizontalAxisName = "Horizontal";// The name given to the horizontal axis for the cross platform input
            public string verticalAxisName = "Vertical";// The name given to the vertical axis for the cross platform input
            public GameObject posController;// Specify the target object to control the character position
            public GameObject camController;// Specify the object to control the camera position
            Vector3 m_StartPos;
            bool m_UseX;// Toggle for using the x axis
            bool m_UseY;// Toggle for using the Y axis
            CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis;// Reference to the joystick in the cross platform input
            CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis;// Reference to the joystick in the cross platform input
            void OnEnable ()
            {
                CreateVirtualAxes ();
            }
            void Start ()
            {m_StartPos = transform.position;
            }
            void UpdateVirtualAxes (Vector3 value)
            {
                var delta = m_StartPos-value;
                delta.y = -delta.y;
                delta/= MovementRange;
                if (m_UseX)
                {
                    m_HorizontalVirtualAxis.Update (delta.x);
                    posController.GetComponent(). inputX = delta.x;
                }
                if (m_UseY)
                {
                    m_VerticalVirtualAxis.Update (delta.y);
                    posController.GetComponent(). inputX = delta.y;
                }
            }
            void CreateVirtualAxes ()
            {
                // set axes to use
                m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
                m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
                // create new axes based on axes to use
                if (m_UseX)
                {
                    m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis (horizontalAxisName);
                    CrossPlatformInputManager.RegisterVirtualAxis (m_HorizontalVirtualAxis);
                }
                if (m_UseY)
                {
                    m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis (verticalAxisName);
                    CrossPlatformInputManager.RegisterVirtualAxis (m_VerticalVirtualAxis);
                }
            }
            public void OnDrag (PointerEventData data)
            {
                Vector3 newPos = Vector3.zero;
                if (m_UseX){
                    int delta = (int) (data.position.x-m_StartPos.x);
                    delta = Mathf.Clamp (delta, -MovementRange, MovementRange);
                    newPos.x = delta;
                }
                if (m_UseY)
                {
                    int delta = (int) (data.position.y-m_StartPos.y);
                    delta = Mathf.Clamp (delta, -MovementRange, MovementRange);
                    newPos.y = delta;
                }
                transform.position = new Vector3 (m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
                UpdateVirtualAxes (transform.position);
            }
            public void OnPointerUp (PointerEventData data)
            {
                transform.position = m_StartPos;
                UpdateVirtualAxes (m_StartPos);
            }
            public void OnPointerDown (PointerEventData data) {}
            void OnDisable ()
            {
                // remove the joysticks from the cross platform input
                if (m_UseX)
                {
                    m_HorizontalVirtualAxis.Remove ();
                }
                if (m_UseY)
                {
                    m_VerticalVirtualAxis.Remove ();
                }
            }
                // float posController = GameObject.FindObjectOfType (ThirdPersonController) as ThirdPersonController;
                //posController.GetComponent().inputX = position.x;
                //posController.GetComponent().inputY = position.y;
        }
    }