Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by borisvdb · Sep 04, 2017 at 08:13 AM · inputcareventsystemverticalhorizontal

Trying to get script to receive input from EventSystem

I have a bit of a convoluted question.

I have a car controller script that I downloaded off of the asset store which comes with it's own control system not like the stock unity one. I'm trying to control the car with the unity mobile joystick using the event system. So I'm trying to make _verticalinput and _horizontalinput be the equivalent of the eventsystems horizontal and vertical inputs.

The public void on 86 to 92 is what I added on myself. Any help would be much appreciated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using System.Linq;
 using System;
 using UnityEngine.EventSystems;
 
 [Serializable]
 public class Controls {
 
     [Tooltip("If this variable is true, the control for this variable will be activated.")]
     public bool enable_reloadScene_Input = true;
     [Tooltip("The key that must be pressed to reload the current scene.")]
     public KeyCode reloadScene = KeyCode.R;
 
     [Space(10)][Tooltip("If this variable is true, the control for this variable will be activated.")]
     public bool enable_startTheVehicle_Input = true;
     [Tooltip("The key that must be pressed to turn the vehicle engine on or off.")]
     public KeyCode startTheVehicle = KeyCode.F;
 
     [Space(10)][Tooltip("If this variable is true, the control for this variable will be activated.")]
     public bool enable_enterEndExit_Input = true;
     [Tooltip("The key that must be pressed to enter or exit the vehicle.")]
     public KeyCode enterEndExit = KeyCode.T;
 
     [Space(10)][Tooltip("If this variable is true, the control for this variable will be activated.")]
     public bool enable_handBrakeInput_Input = true;
     [Tooltip("The key that must be pressed to activate or deactivate the vehicle hand brake.")]
     public KeyCode handBrakeInput = KeyCode.Space;
 
     [Space(10)][Tooltip("If this variable is true, the control for this variable will be activated.")]
     public bool enable_switchingCameras_Input = true;
     [Tooltip("The key that must be pressed to toggle between the cameras of the vehicle.")]
     public KeyCode switchingCameras = KeyCode.C;
 }
 
 public class MSSceneController : MonoBehaviour, IMoveHandler {
 
     #region defineInputs
 
     [Tooltip("Vertical input recognized by the system")]
     private string _verticalInput;
 
     [Tooltip("Horizontal input recognized by the system")]
     private string _horizontalInput;
 
     [Tooltip("Horizontal input for camera movements")]
     public string _mouseXInput = "Mouse X";
 
     [Tooltip("Vertical input for camera movements")]
     public string _mouseYInput = "Mouse Y";
 
     [Tooltip("Scroll input, to zoom in and out of the cameras.")]
     public string _mouseScrollWheelInput = "Mouse ScrollWheel";
     #endregion
 
     [Space(10)][Tooltip("Here you can configure the vehicle controls, choose the desired inputs and also, deactivate the unwanted ones.")]
     public Controls controls;
     [Tooltip("All vehicles in the scene containing the 'MS Vehicle Controller' component must be associated with this list.")]
     public GameObject[] vehicles;
     [Space(10)][Tooltip("This variable is responsible for defining the vehicle in which the player will start. It represents an index of the 'vehicles' list, where the number placed here represents the index of the list. The selected index will be the starting vehicle.")]
     public int startingVehicle = 0;
     [Tooltip("The player must be associated with this variable. This variable should only be used if your scene also has a player other than a vehicle. This \"player\" will temporarily be disabled when you get in a vehicle, and will be activated again when you leave a vehicle.")]
     public GameObject player;
     [Tooltip("If this variable is true and if you have a player associated with the 'player' variable, the game will start at the player. Otherwise, the game will start in the starting vehicle, selected in the variable 'startingVehicle'.")]
     public bool startInPlayer = false;
     [Tooltip("This is the minimum distance the player needs to be in relation to the door of a vehicle, to interact with it.")]
     public float minDistance = 3;
     [Space(10)][Tooltip("If this variable is true, useful data will appear on the screen, such as the car's current gear, speed, brakes, among other things.")]
     public bool GUIVisualizer = true;
 
     #region customizeInputs
     [HideInInspector]
     public float verticalInput = 0;
     [HideInInspector]
     public float horizontalInput = 0;
     [HideInInspector]
     public float mouseXInput = 0;
     [HideInInspector]
     public float mouseYInput = 0;
     [HideInInspector]
     public float mouseScrollWheelInput = 0;
     #endregion
 
     public void OnMove (AxisEventData eventData){
 
         #region customizeInputsValues
         _verticalInput = eventData.moveVector.y.ToString();
         _horizontalInput = eventData.moveVector.x.ToString();
         #endregion
     }
 
     int currentVehicle = 0;
     int clampGear;
     int proximityObjectIndex;
     int proximityDoorIndex;
     bool blockedInteraction = false;
     bool error;
     string sceneName;
     string gear;
     string velocitykmh;
     string velocityms;
     string velocitymph;
     string handBrake;
     MSVehicleController vehicleCode;
     MSVehicleController controllerTemp;
     float currentDistanceTemp;
     float proximityDistanceTemp;
 
     void Awake () {
         CheckEqualKeyCodes ();
         Time.timeScale = 1;
         error = false;
         sceneName = SceneManager.GetActiveScene ().name;
         currentVehicle = startingVehicle;
 
         if (startingVehicle >= vehicles.Length) {
             error = true;
             Debug.LogError ("Vehicle selected to start does not exist in the 'vehicles' list");
         }
         for (int x = 0; x < vehicles.Length; x++) {
             if (vehicles [x]) {
                 if (!vehicles [x].GetComponent<MSVehicleController> ()) {
                     error = true;
                     Debug.LogError ("The vehicle associated with the index " + x + " does not have the 'MSVehicleController' component. So it will be disabled.");
                 }
             }else{
                 error = true;
                 Debug.LogError ("No vehicle was associated with the index " + x + " of the vehicle list.");
             }
         }
         if (error) {
             for (int x = 0; x < vehicles.Length; x++) {
                 if (vehicles [x]) {
                     MSVehicleController component = vehicles [x].GetComponent<MSVehicleController> ();
                     if (component) {
                         component.disableVehicle = true;
                     }
                     vehicles [x].SetActive (false);
                 }
             }
             return;
         }
         //
         for (int x = 0; x < vehicles.Length; x++) {
             if (vehicles [x]) {
                 vehicles [x].GetComponent<MSVehicleController> ().isInsideTheCar = false;
             }
         }
         if (player) {
             player.SetActive (false);
         }
         if (startInPlayer) {
             if (player) {
                 player.SetActive (true);
             } else {
                 startInPlayer = false;
                 if (vehicles.Length > startingVehicle && vehicles [currentVehicle]) {
                     vehicles [startingVehicle].GetComponent<MSVehicleController> ().isInsideTheCar = true;
                 }
             }
         } else {
             if (vehicles.Length > startingVehicle && vehicles [currentVehicle]) {
                 vehicles [startingVehicle].GetComponent<MSVehicleController> ().isInsideTheCar = true;
             }
         }
     }
 
     void CheckEqualKeyCodes(){
         var type = typeof(Controls);
         var fields = type.GetFields();
         var values = (from field in fields
             where field.FieldType == typeof(KeyCode)
             select (KeyCode)field.GetValue(controls)).ToArray();
 
         foreach(var value in values)
             if(Array.FindAll(values, (a) => { return a == value; }).Length > 1)
                 Debug.LogError ("There are similar commands in the 'controls' list. Use different keys for each command.");
     }
 
     void Update () {
         
         if (!error) {
             #region customizeInputsValues
             verticalInput = Input.GetAxis (_verticalInput);
             horizontalInput = Input.GetAxis (_horizontalInput);
             mouseXInput = Input.GetAxis (_mouseXInput);
             mouseYInput = Input.GetAxis (_mouseYInput);
             mouseScrollWheelInput = Input.GetAxis (_mouseScrollWheelInput);
             #endregion 
 
             vehicleCode = vehicles [currentVehicle].GetComponent<MSVehicleController> ();
 
             if (Input.GetKeyDown (controls.reloadScene) && controls.enable_reloadScene_Input) {
                 SceneManager.LoadScene (sceneName);
             }
 
             if (Input.GetKeyDown (controls.enterEndExit) && !blockedInteraction && player && controls.enable_enterEndExit_Input) {
                 if (vehicles.Length <= 1) {
                     if (vehicleCode.isInsideTheCar) {
                         vehicleCode.ExitTheVehicle ();
                         if (player) {
                             player.SetActive (true);
                             if (vehicleCode.doorPosition[0].transform.position != vehicles [currentVehicle].transform.position) {
                                 player.transform.position = vehicleCode.doorPosition[0].transform.position;
                             } else {
                                 player.transform.position = vehicleCode.doorPosition[0].transform.position + Vector3.up * 3.0f;
                             }
                         }
                         blockedInteraction = true;
                         StartCoroutine ("WaitToInteract");
                     } else {
                         currentDistanceTemp = Vector3.Distance (player.transform.position, vehicleCode.doorPosition[0].transform.position);
                         if (currentDistanceTemp < minDistance) {
                             vehicleCode.EnterInVehicle ();
                             if (player) {
                                 player.SetActive (false);
                             }
                             blockedInteraction = true;
                             StartCoroutine ("WaitToInteract");
                         }
                     }
                 } else {
                     proximityObjectIndex = 0;
                     proximityDoorIndex = 0;
                     for (int x = 0; x < vehicles.Length; x++) {
                         controllerTemp = vehicles [x].GetComponent<MSVehicleController> ();
 
                         for (int y = 0; y < controllerTemp.doorPosition.Length; y++) {
                             currentDistanceTemp = Vector3.Distance (player.transform.position, controllerTemp.doorPosition[y].transform.position);
                             proximityDistanceTemp = Vector3.Distance (player.transform.position, vehicles [proximityObjectIndex].GetComponent<MSVehicleController> ().doorPosition[proximityDoorIndex].transform.position);
                             if (currentDistanceTemp < proximityDistanceTemp) {
                                 proximityObjectIndex = x;
                                 proximityDoorIndex = y;
                             }
                         }
 
                     }
                     
                     if (vehicleCode.isInsideTheCar) {
                         vehicleCode.ExitTheVehicle ();
                         if (player) {
                             player.SetActive (true);
                             if (vehicleCode.doorPosition[0].transform.position != vehicles [currentVehicle].transform.position) {
                                 player.transform.position = vehicleCode.doorPosition[0].transform.position;
                             } else {
                                 player.transform.position = vehicleCode.doorPosition[0].transform.position + Vector3.up * 3.0f;
                             }
                         }
                         blockedInteraction = true;
                         StartCoroutine ("WaitToInteract");
                     } else {
                         controllerTemp = vehicles [proximityObjectIndex].GetComponent<MSVehicleController> ();
                         proximityDistanceTemp = Vector3.Distance (player.transform.position, controllerTemp.doorPosition[0].transform.position);
                         for (int x = 0; x < controllerTemp.doorPosition.Length; x++) {
                             currentDistanceTemp = Vector3.Distance (player.transform.position, controllerTemp.doorPosition [x].transform.position);
                             if (currentDistanceTemp < proximityDistanceTemp) {
                                 proximityDistanceTemp = currentDistanceTemp;
                             }
                         }
                         if (proximityDistanceTemp < minDistance) {
                             currentVehicle = proximityObjectIndex;
                             vehicles [currentVehicle].GetComponent<MSVehicleController> ().EnterInVehicle ();
                             if (player) {
                                 player.SetActive (false);
                             }
                             blockedInteraction = true;
                             StartCoroutine ("WaitToInteract");
                         }
                     }
                 }
             }
 
             gear = "Gear: " + vehicleCode.currentGear;
             velocitykmh = "Velocity(km/h): " + (int)(vehicleCode.KMh * clampGear);
             velocityms = "Velocity(m/s): " + (int)((vehicleCode.KMh / 3.6f) * clampGear);
             velocitymph = "Velocity(mp/h): " + (int)(vehicleCode.KMh * 0.621371f * clampGear);
             handBrake = "HandBreak: " + vehicleCode.handBrakeTrue;
         }
     }
 
     IEnumerator WaitToInteract(){
         yield return new WaitForSeconds (0.7f);
         blockedInteraction = false;
     }
 
     void EnableVehicle(int index){
         currentVehicle = Mathf.Clamp (currentVehicle, 0, vehicles.Length-1);
         if (index != currentVehicle) {
             if (vehicles [currentVehicle]) {
                 for (int x = 0; x < vehicles.Length; x++) {
                     vehicles [x].GetComponent<MSVehicleController> ().ExitTheVehicle ();
                 }
                 vehicles [currentVehicle].GetComponent<MSVehicleController> ().EnterInVehicle ();
             }
         }
     }
 
     void OnGUI(){
         if (!error) {
             if (vehicles.Length > 1 && !player) {
                 if (GUI.Button (new Rect (5, 5, 50, 40), "<<")) {
                     currentVehicle--;
                     EnableVehicle (currentVehicle + 1);
                 }
                 if (GUI.Button (new Rect (60, 5, 50, 40), ">>")) {
                     currentVehicle++;
                     EnableVehicle (currentVehicle - 1);
                 }
             }
             if (vehicles.Length > 0 && currentVehicle < vehicles.Length && GUIVisualizer && vehicleCode) {
                 if (vehicleCode.isInsideTheCar) {
                     GUI.Box (new Rect (5, 50, 180, 105), "");
                     clampGear = Mathf.Clamp (vehicleCode.currentGear, -1, 1);
                     if (clampGear == 0) {
                         clampGear = 1;
                     }
 
                     GUI.skin.label.fontSize = 14;
                     GUI.Label (new Rect (10, 50, 300, 35), gear);
                     GUI.Label (new Rect (10, 70, 300, 35), velocitykmh);
                     GUI.Label (new Rect (10, 90, 300, 35), velocityms);
                     GUI.Label (new Rect (10, 110, 300, 35), velocitymph);
                     GUI.Label (new Rect (10, 130, 300, 35), handBrake);
                 }
             }
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by F11BOT · Sep 28, 2017 at 06:49 PM

I am not a professional programmer or developer, I'm just an curious guy but I found the solution at least for me. Basically you have to implement the crossplatforminput from the standart assets into the vehicle controller and then change the input like this:

         verticalInput = CrossPlatformInputManager.GetAxis("Vertical");
         horizontalInput = CrossPlatformInputManager.GetAxis("Horizontal");
         mouseXInput = controls.mouseXInput;
         mouseYInput = controls.mouseYInput;
         mouseScrollWheelInput = controls.mouseScrollWheelInput;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image borisvdb · Sep 28, 2017 at 09:43 PM 0
Share

Don't worry, I'm not either. It's just a hobby for now. I'll try that method next time I access my build.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

123 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem when using the same key for different actions 0 Answers

Detect if mouse has clicked on a UI element? 0 Answers

Horizontal/Vertical Layout Group forcing control? 0 Answers

New Input System issue with UNet/Mirror Multiplayer 1 Answer

Horizontal Input showing random decimal when it should be 0 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges