- Home /
Mouse slingshot to touch control problem
I have a beautiful script adapted from a sidescrollling GUI script that lets me make a FPS slingshot GUI which has rotation control and adjusts tension according to how far you pull the finger away. It works beautifully on the computer but on touchscreen I have the much documented (Touch)MouseOrbit problem with some problem ( that I cant quite put my finger on) between the last finger touch and the new finger touch. A previous answerer suggested that I should look at .: .......TouchPhase.Began....... Then move it only in TouchPhase.Moved or Stationary with Quaternion.Lerp(...)
but i cannot think hot to implement it.... hope someone can help... here is my script....:
#pragma strict
var loadedobject : GameObject;
var barHeight : int = 32;
var TargetBack : GUIStyle;
var projectile : Rigidbody;
var spacer : int = 8;
var catapault : GameObject;
var tiles : Tile[]; // home-made class. see the end of script.
var springSpeed : float = 5.0;
var throwSpeed : float = 8.0;
var strengthx : float;
var strengthy : float;
var strength : float;
var ThrowAngle : float;
var loaded : boolean;
var tension : float;
var sling : GameObject;
var spawnpoint : GameObject;
var tester : GUIStyle;
var tileSize : float;
private var screenWidth : int = Screen.width;
private var screenHeight : int = Screen.height;
private var slidePosition : Vector3;
private var maxSlidePosition : Vector3;
private var oldMousePosition : Vector3;
private var throwPosition : Vector3;
function Start () {
// in a real senario maybe this is dynamically created from a database. for now, here are some blanks.
tiles = [new Tile()];
oldMousePosition = Input.mousePosition;
}
function Update () {
catapault.transform.rotation.x=0;
catapault.transform.rotation.z=0;
if (!Input.anyKey) {
slidePosition = Vector2.Lerp(slidePosition, Vector2((Screen.width/2)-tileSize/2,Screen.height/1.5), Time.deltaTime * springSpeed);
oldMousePosition = Input.mousePosition;
}
if((Input.GetMouseButton(0))&&(new Rect(Screen.width/3,0,Screen.width/3,Screen.height/2).Contains(Input.mousePosition)))
{
slidePosition.x += Input.mousePosition.x - oldMousePosition.x;
slidePosition.y += (Input.mousePosition.y - oldMousePosition.y)*-1;
oldMousePosition = Input.mousePosition;
}
else if((Input.GetMouseButtonUp(0))&&(new Rect(0,0,Screen.width,Screen.height/3).Contains(Input.mousePosition))){
Missile();
slidePosition = Vector2.Lerp(slidePosition, Vector2((Screen.width/2)-tileSize/2,Screen.height/1.5), Time.deltaTime * springSpeed);
}
if(slidePosition.x<Screen.width/2-tileSize/2){
strengthx = Screen.width/2-slidePosition.x-tileSize/2;
ThrowAngle=strengthx/strengthy*-1;}
else {
strengthx = slidePosition.x-Screen.width/2+tileSize/2;
ThrowAngle=strengthx/strengthy;}
if(slidePosition.y<Screen.height/2){
strengthy=Screen.height/2-slidePosition.y;
;}
else{
strengthy=slidePosition.y-Screen.height/2;
}
strength=Mathf.Sqrt((strengthx*strengthx)+(strengthy*strengthy));
catapault.transform.localEulerAngles.y=ThrowAngle*-45;
tension = strength/250;
tileSize = Screen.width/10;
if(strength>0){
sling.transform.localScale=Vector3(1,1,tension);
}
}
function OnGUI () {
screenWidth = Screen.width;
screenHeight = Screen.height;
var xMax : int = Mathf.CeilToInt(tiles.length/3.0f);
maxSlidePosition.x = -spacer*(xMax+1)-tileSize*(xMax)+screenWidth;
for (var i=0; i<tiles.length; i++){
var position : float = i+1;
var xGridPosition : int = Mathf.CeilToInt(position/3.0f);
var yGridPosition : int = Mathf.CeilToInt(position/3.0f);
var rect : Rect = new Rect(spacer*xGridPosition+tileSize*(xGridPosition-1)+slidePosition.x, spacer*xGridPosition+tileSize*(xGridPosition-1)+slidePosition.y, tileSize, tileSize);
var tile : Tile = tiles[i];
GUI.Box(rect, tile.name);
}
GUI.Box(Rect(Screen.width/3, Screen.height/1.5,Screen.width/3, Screen.height/2),"", TargetBack);
}
function Missile(){
var clone : Rigidbody;
clone = Instantiate(projectile, spawnpoint.transform.position, catapault.transform.rotation);
clone.velocity = catapault.transform.TransformDirection (Vector3.forward*strength*.1);
loadedobject.SetActiveRecursively(false);
yield WaitForSeconds(1);
loadedobject.SetActiveRecursively(true);
}
class Tile{
var name : String = "indigo button";
var texture : Texture2D;
by no means easy. this info may help
http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html
Do you realise you're not actually using Touch on the code you pasted in? You won't be able to make it with the others
you'll need to master this Documentation/Documentation/ScriptReference/Touch.html
"can't quite put my finger on" Funny :)