Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by RealLifeSpartan · Apr 20, 2014 at 07:30 PM · inputmobiletouchkeyboard

Changing keyboard input to touch in script

Hi,

I have acquired this script from an asset I bought and I would like to turn the keyboard inputs to touch, be it either touch button or joystick. Upon looking at the script, it appears that many of the other components of the script are linked to the movement part of it. Therefore the script looks "delicate" to me, and I mainly code in JS so I have no clue what to do here. So can anyone take this script and change the movement input to touch, whilst keeping the links between the other components? Thanks

 using UnityEngine;
 using System.Collections;
 
 public class Probe : MonoBehaviour {
 
     [SerializeField] float m_thrustForce = 10;
     [SerializeField] Transform m_thrusterRoot;
     [SerializeField] ParticleSystem m_thruster;
     [SerializeField] Animation m_eyeAnimation;
     [SerializeField] AnimationClip m_eyeAliveAnimationClip;
     [SerializeField] AnimationClip m_eyeDeathAnimationClip;
     [SerializeField] Transform m_pupil;
     [SerializeField] float m_maxPupilDistance;
     [SerializeField] float m_probeVelocityForMaxPupilDistance;
     
     [SerializeField] AudioClip[] m_chirps;
     [SerializeField] AudioClip[] m_deathCries;
     [SerializeField] AudioClip[] m_impactNoises;
     
     [SerializeField] Collider m_rechargeTrigger;
     
     [SerializeField] Renderer m_superGlow;
     
     [SerializeField] AudioSource m_warningAudio;
 
     //___________________________________________________/   Public STATIC \__
     
     public static Probe CurrentProbe;
     
     //__________________________________________________/           Public \__
     
     public float Fuel {
         get;set;
     }
     
     public bool Dead {
         get {
             return m_dead;
         }
     }
     
     public bool Dying {
         get{
             return m_dying;
         }
     }
     
     public bool SUPER {
         get;set;
     }
     
     public bool Revived {
         get{
             return m_revived;
         }
         set{
             if(value && !m_revived){
                 m_dead = false;
                 m_dying = false;
                 PlayChirp(3);
                 transform.rotation = Quaternion.identity;
                 rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionZ;
                 this.gameObject.layer = LayerMask.NameToLayer("Revived");
                 StartCoroutine(Blinking());
             }
             m_revived = value;
         }
     }
     protected bool m_revived;
     
     public bool Paused {
         get;set;
     }
     
     //__________________________________________________/   Private STATIC \__
     
     //protected static Probe s_currentProbe;
     
     //__________________________________________________/          Private \__
     
     protected bool m_dying;
     protected bool m_dead;
     
     //__________________________________________________/    Monobehaviour \__
 
     private void Awake(){
         CurrentProbe = this;
     }
 
     private void Start() {
         PlayChirp(3);
         Fuel = 10;
         StartCoroutine(Blinking());
     }
     
     private IEnumerator Blinking(){
         
         m_eyeAnimation.clip = m_eyeAliveAnimationClip;
         m_eyeAnimation.Play();
         
         while(true){
             if(Random.Range(0,7)==0){
                 PlayChirp(1);
             }
             m_eyeAnimation.Play();
             float duration = Random.Range(0.2f, 4.0f);
             for (float f = 0; f < duration; f+=Time.deltaTime) {
                 yield return 1;
                 if(Fuel == 0){
                     yield break;
                 }
             }
         }
     }
 
     private void Update() {
         
         if(SUPER){
             m_superGlow.enabled = true;
         }
         
         if(SUPER || Revived){
             Fuel = 10;
             m_rechargeTrigger.gameObject.SetActive(true);
         }
         
         float inputX = 0;
         float inputY = 0;
         bool thrusting = false;
         
         if(Fuel < 1.5f && !m_dying){
             m_warningAudio.volume = 0.5f;
         }else{
             m_warningAudio.volume = 0.0f;
         }
         
         if(Probe.CurrentProbe == this){
         
             inputX = Input.GetAxisRaw("Horizontal");
             inputY = Input.GetAxisRaw("Vertical");
         
             thrusting = (inputX != 0 || inputY != 0) && Fuel > 0 && !CameraFollower.ShowingMap && !Paused;
             
         }else{
             
             if(Revived){
             
                 Vector3 toCurrent = Probe.CurrentProbe.transform.position - transform.position;
             
                 toCurrent += Random.insideUnitSphere; // a little tweak;
                 
                 toCurrent.z = 0;
             
                 inputX = toCurrent.normalized.x;
                 inputY = toCurrent.normalized.y;
                 
                 thrusting = toCurrent.magnitude > 1.66f;
             
             }
             
         }
                 
         Vector3 inputVector = new Vector3(inputX, inputY, 0).normalized;
         
         //Movement
         if(thrusting){
             rigidbody.AddForce(inputVector * m_thrustForce);                
         }
     
         //Thrusters
         if(thrusting){
             m_thrusterRoot.transform.LookAt(transform.position - inputVector, Vector3.forward);
         }
 
         if( thrusting && !m_thruster.isPlaying ){
             m_thruster.Play();
         }    
         if( !thrusting && m_thruster.isPlaying ){
             m_thruster.Stop();
         }
     
         if(thrusting){
             audio.volume += Time.deltaTime * 10;
         }else{
             audio.volume -= Time.deltaTime * 10;
         }
     
     
         //Fuel Consumption
         if(thrusting){
             Fuel = Mathf.Max(0, Fuel - Time.deltaTime);
         }
     
         //Death
         if(Fuel == 0 && !m_dying && !SUPER){
             StartCoroutine(Die());
         }
         
         //Pupil
         if(!m_dying){
             float pupilDistanceFactor = Mathf.InverseLerp(0, m_probeVelocityForMaxPupilDistance, rigidbody.velocity.magnitude);
             m_pupil.localPosition = rigidbody.velocity.normalized * pupilDistanceFactor * m_maxPupilDistance;
         }        
         
     }
     
     protected void OnCollisionEnter(Collision c){
         float mag = c.relativeVelocity.magnitude;
         float volume = Mathf.InverseLerp(0.5f, 8, mag);
         AudioSource.PlayClipAtPoint(m_impactNoises[Random.Range(0,m_impactNoises.Length)], Vector3.zero, volume);
         if(mag > 2 && !m_dying){
             PlayChirp(1);
         }
     }
     
     protected IEnumerator Die(){
 
         m_dying = true;
         
         yield return new WaitForSeconds(1f);
         
         AudioSource.PlayClipAtPoint(m_deathCries[Random.Range(0,m_deathCries.Length)], Vector3.zero);
         
         rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezePositionZ;
 
         m_eyeAnimation.clip = m_eyeDeathAnimationClip;
         m_eyeAnimation.Play();        
 
         yield return new WaitForSeconds(3);
         
         m_dead = true;
 
         yield break;
     
     }
     
     protected void PlayChirp(int n){
         StartCoroutine(_PlayChirp(n));
     }
     
     protected IEnumerator _PlayChirp(int n){
         for (int i = 0; i < n; i++) {
             AudioClip clip = m_chirps[Random.Range(0, m_chirps.Length)];
             AudioSource.PlayClipAtPoint(clip, Vector3.zero, 0.33f);
             yield return new WaitForSeconds(clip.length);
         }
 
     }
 
 }
 
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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Is it possible to have the Unity TouchScreenKeyboard permanently open? 0 Answers

How to make responsive Touch inputs? 1 Answer

What's the BEST: most efficient, reliable, cross-platform way to detect a touch/tap on mobile (Android-iOS) on several 2D colliders in a scene while still retaining desktop compatibility and raycast/touch masking? 0 Answers

Android: TouchScreenKeyboard not functioning 2 Answers

Mobile game touch problem (C#) 2 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