Other
How to add touch drag controls on Space Shooter?
So i've completed Space Shooter tutorial, also i finished tutorial on how to port it on mobile device, everything works fine except touch controls showed in tutorial doesn't fit me well, it's like it emulates joystick and ship sometimes goes out of control. Can someone please hint me some answers on how can i change player controller script to add drag feature please?
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Done_Boundary
{
public float xMin, xMax, zMin, zMax;
}
public class Done_PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Done_Boundary boundary;
public GameObject shot;
public Transform shotSpawn;
public float fireRate;
public SimpleTouchPad touchPad;
public SimpleTouchAreaButton areaButton;
private float nextFire;
private Quaternion calibrationQuaternion;
void Start()
{
CalibrateAccelerometer();
}
void Update ()
{
if (areaButton.CanFire () && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
GetComponent<AudioSource>().Play ();
}
}
//Used to calibrate the Iput.acceleration input
void CalibrateAccelerometer()
{
Vector3 accelerationSnapshot = Input.acceleration;
Quaternion rotateQuaternion = Quaternion.FromToRotation(new Vector3(0.0f, 0.0f, -1.0f), accelerationSnapshot);
calibrationQuaternion = Quaternion.Inverse(rotateQuaternion);
}
//Get the 'calibrated' value from the Input
Vector3 FixAcceleration(Vector3 acceleration)
{
Vector3 fixedAcceleration = calibrationQuaternion * acceleration;
return fixedAcceleration;
}
void FixedUpdate ()
{
// float moveHorizontal = Input.GetAxis ("Horizontal");
// float moveVertical = Input.GetAxis ("Vertical");
// Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
// Vector3 accelerationRaw = Input.acceleration;
// Vector3 acceleration = FixAcceleration (accelerationRaw);
// Vector3 movement = new Vector3 (acceleration.x, 0.0f, acceleration.y);
Vector2 direction = touchPad.GetDirection();
Vector3 movement = new Vector3(direction.x, 0.0f, direction.y);
GetComponent<Rigidbody>().velocity = movement * speed;
GetComponent<Rigidbody>().position = new Vector3
(
Mathf.Clamp (GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
);
GetComponent<Rigidbody>().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent<Rigidbody>().velocity.x * -tilt);
}
}
Answer by RecyclingBen · Mar 15, 2017 at 11:43 AM
If you want to make your ship follow your finger's position you can set up an empty gameobject with a public static Vector3 saying it's position. Then set its position to equal wherever your finger is located, something like using Unity
public static Vector3 finger; public static Vector3 fingerPosition; void Update() { foreach (Touch touch in Input.Touches) { finger = touch.position; fingerPosition = camera.ScreenToWorldPoint(finger); } transform.position = fingerPosition; }
and then set the ship to move towards this position (I'm sure there are problems in my code, it's untested, but I hope you get the gist of what I'm saying.
Thanks, I'll definitely try. Basically i just need to assign player to empty gameobject with this example script, right?
Follow this Question
Related Questions
Problem with NewInputSystem version 1.0.0 - preview.7 0 Answers
How to Make a Train like movement controller 0 Answers
Gamepad rumble independent motors 0 Answers
Space shooter tutorial, space ship keep moving and has interia 1 Answer
My bolt isn't respawning in game Its driving me crazy i have been at it for 2 days !! 1 Answer