- Home /
How can i change the player controls in Space Shooter to Touch.
Hello, I am new to coding and looking for some help on trying to convert the Space Shooter Tutorial player controls to touch. If anyone could help me I would really appreciate it. I have referred to the unity references, but i don't understand how to put it all together. Thank you and hope to hear from you.
Answer by Guntereno · Apr 05, 2014 at 02:40 PM
Hi there,
I've just been through the Space Shooter sample myself and wanted to do the same thing. This is what I ended up with as my FixedUpdate() function. I wanted it to work with mouse or touch so it worked in the game view too. I basically set a target position whenever the user touches, and set the velocity to move towards the target. I've also exposed a public dampingRadius so the ship can slow down as it reaches its target.
using UnityEngine; using System.Collections;
[System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; }
public class PlayerController : MonoBehaviour {
public Boundary boundary;
public float dampingRadius;
public float fireDelay;
public GameObject shot;
public Transform shotSpawn;
public float speed;
public float tilt;
public float velocityLag;
private Vector3 target;
private float nextFire;
void Start () {
}
void Update() {
bool triggered = false;
if (Input.mousePresent && Input.GetMouseButton(0))
{
triggered = true;
}
else if (Input.touchCount > 0)
{
triggered = true;
}
if (triggered && (Time.time > nextFire))
{
nextFire = Time.time + fireDelay;
Instantiate(shot, shotSpawn.position, Quaternion.identity);
audio.Play();
}
}
void FixedUpdate ()
{
Vector3? touchPos = null;
if (Input.mousePresent && Input.GetMouseButton(0))
{
touchPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0.0f);
}
else if(Input.touchCount > 0)
{
touchPos = new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0.0f);
}
if (touchPos != null)
{
target = Camera.main.ScreenToWorldPoint(touchPos.Value);
target.y = rigidbody.position.y;
}
Vector3 offset = target - rigidbody.position;
float magnitude = offset.magnitude;
if (magnitude > dampingRadius)
magnitude = dampingRadius;
float dampening = magnitude / dampingRadius;
Vector3 desiredVelocity = offset.normalized * speed * dampening;
rigidbody.velocity += (desiredVelocity - rigidbody.velocity) * velocityLag;
// Clamp to bounds
rigidbody.position = new Vector3
(
Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)
);
rigidbody.rotation = Quaternion.Euler(0.0f, 0.0f, rigidbody.velocity.x * -tilt);
}
}
Hi,
I tried this code, but I'm not sure what the target is set too in the first place? Is it a global variable, if so which value is used? Similar with:dampingRadius & velocityLag.
do you have the full code for this?
Regards
Hi there,
Sorry, I didn't paste the whole code. I'll post it when I get home tonight.
"target" is a member variable of the $$anonymous$$onoBehaviour. As it's only updated when the player touches (or presses the mouse button) the ship will continue moving towards the stored target position.
Hi Guntereno,
Any update on this?
It would really help me if you can provide that full code or even project in a zip.
It would be great if you can attach to this ticket or my email: e.lukiyanov@edu.salford.ac.uk
Regards Evgeny
Hi,
Also keen to see the full code for this touch movement! Would love for anyone to also share it if they get anything!
Perfect - took me a few moments to realise I needed to declare target as private and dampeningRadius and velocityLag as public variables but works perfectly. I have a touch screen monitor and saw both touch and key controls working!
Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613134430im_/https://answers.unity.com/themes/thub/images/avi.jpg)