- Home /
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