- Home /
Joystick issues
Hi, I've found a nice script here - http://www.joshuagamedev.com/on-screen-joystick-test/ and it's throwing me an error that I can't seem to fix :( Can anyone help?
Thanks in advance
// input variables public var baseTexture:Texture;// the base texture, usually the touchable area. public var joystickTexture:Texture ;// joystick texture, nothing but visual representation. public var touchArea:Rect;// the limits and size of touch action area. public var circleRadius:float;//joystick limit radius, it cannot move beyond this. public var invertY:boolean;// inverts the vertical output.
//output variables | gives values from -1 to 1.
static public var horizontal:float;
static public var vertical:float;
//calculation variables
private var joystick:Rect;
private var cursorPosition:Vector2;//x,y position
of cursor/touch private var touchAreaCenter:Vector2;//stores the center of the touchable area. private var cursorDifference:Vector2;//the difference from cursor to center of touchable area. private var cursorDistance:float;//the distance from cursor and the center of touchable area. private var joystickPosition:Vector2;//position of the joystick.
function Start()
{
// sets the touch area size to texture size if none is given.
if ((touchArea.width || touchArea.height) == 0)
{
touchArea.width = baseTexture.width;
touchArea.height = baseTexture.height;
joystick.width = joystickTexture.width;
joystick.height = joystickTexture.height;
}
touchArea.y = Screen.height - touchArea.height;// aligns the
touchable area to bottom left. touchAreaCenter = Vector2(touchArea.x + touchArea.width / 2, touchArea.y + touchArea.height / 2); }
function Update ()
{
// screen space starts from bottom left.
cursorPosition = ScreenToGUIPoint(Input.mousePosition);//
converts screen space to GUI space.
cursorDifference = cursorPosition - touchAreaCenter;
cursorDistance = cursorDifference.magnitude;
joystickPosition = Vector2(joystick.x, joystick.y);
// if a touch is detected then execute else reset joystick to center
of touch area if (Input.GetMouseButton(0) && touchArea.Contains(cursorPosition)) { // if the cursor is outside the circle radius, joystick is locked at the circle's circumference. if (cursorDistance circleRadius) { joystickPosition = touchAreaCenter + (cursorDifference / cursorDistance) * circleRadius; joystick.x = joystickPosition.x - joystick.width / 2; joystick.y = joystickPosition.y - joystick.height / 2; } else { joystickPosition = cursorPosition; joystick.x = cursorPosition.x - joystick.width / 2; joystick.y = cursorPosition.y - joystick.height / 2; }
// converts the position of joystick(-1 to 1) using the circle circumference as limits and not the limits of toucharea. horizontal = Interpolate(joystickPosition.x, touchAreaCenter.x - circleRadius, touchAreaCenter.x + circleRadius, -1, 1); vertical = Interpolate(joystickPosition.y, touchAreaCenter.y + circleRadius, touchAreaCenter.y - circleRadius, invertY?1:-1, invertY?-1:1); } else { joystick.x = touchAreaCenter.x - joystick.width / 2; joystick.y = touchAreaCenter.y - joystick.width / 2; horizontal = vertical = 0; } }
function OnGUI()
{
GUI.DrawTexture(touchArea, baseTexture, ScaleMode.ScaleAndCrop,
true); GUI.DrawTexture(joystick, joystickTexture, ScaleMode.ScaleAndCrop, true); }
function ScreenToGUIPoint(input:Vector2):Vector2
{
return Vector2(input.x, Interpolate(input.y, Screen.height, 0,
0, Screen.height)); }
private function Interpolate(xVar, xMin, xMax, yMin, yMax):float
{
return ( (xVar - xMin) / (xMax - xMin) ) * (yMax - yMin) + yMin;
}
^ It's the last section here Assets/OnScreenJoystick.js Operator '-' cannot be used with a left hand side of type 'Object' and a right hand side of type 'Object'.
Why dont you use the original joystick script which Unity provides with the Penelope tutorial?
Because I could never really get it to work - and it seemed really huge for what I wanted to do, (super complex) and I want (eventually) to have 2 joysticks controlling two different things, and it'd help if I understood the code I'm working with :<
I might give it another go in the meantime though
well, your problem lies on line 96 character 24 and the problem is that the script is subtratcing an object to an object (no possible) now the problem is which one is line 96...
You want one joystick for moving and rotating right? Trust me just attach penelope joystick.js to both of them
All you have to do --> Attach joystick script to the 2 joysticks
and 3rd person controller script to your player and asign variabless
Dont even need to open the script except you want to add stuff
I'll read your code anyways
private function Interpolate(xVar, x$$anonymous$$in, x$$anonymous$$ax, y$$anonymous$$in, y$$anonymous$$ax):float { return ( (xVar - x$$anonymous$$in) / (x$$anonymous$$ax - x$$anonymous$$in) ) * (y$$anonymous$$ax - y$$anonymous$$in) + y$$anonymous$$in; }
I beleive. Thanks <3
Answer by aldonaletto · Mar 16, 2012 at 12:08 PM
This error message indicates that you're trying to subtract two variables of unknown type (which the compiler calls object - not the same as Object, an Unity base class). The Interpolate function declaration doesn't define the parameter types, what probably is causing this error. Change it like this:
private function Interpolate(xVar:float, xMin:float, xMax:float, yMin:float, yMax:float):float { return ( (xVar - xMin) / (xMax - xMin) ) * (yMax - yMin) + yMin; }
Thankyou very much :) It's stopped the error. But it doesn't work with my script I've put on my character -
function Update () { rigidbody.AddForce((Vector3.up 1) Joystick2.vertical); rigidbody.AddForce((-Vector3.right 1) Joystick2.horizontal); rigidbody.AddForce((-Vector3.down 1) Joystick2.vertical); rigidbody.AddForce((Vector3.left* 1) * Joystick2.horizontal); }
rigidbody.force assign attempt for 'Player 1' is not valid. Input position is { NaN, NaN, NaN }. UnityEngine.Rigidbody:AddForce(Vector3) $$anonymous$$ovement Joy:Update() (at Assets/$$anonymous$$ovement Joy.js:6)
If you can work out why, I'd be ever so, ever so greatful. Thanks for being awesome everyone that helped :)
It seems that Joystick2.horizontal and/or Joystick2.vertical contain garbage, what is causing the NaN values. But what is Joystick2? It doesn't exist in the script above, thus nobody can tell what's wrong. You should post a new question with the relevant script (Joystick2, or the script where it's created and modified).
Anyway, the way you were applying forces would not work - that's how you could do the job:
function Update () { var force = Vector3.up * Joystick2.vertical + Vector3.right * Joystick2.horizontal; rigidbody.AddForce(force); }
Joystick2 is the name of the file I couldnt get to work originally.
Thanks for being super :)
I thought it would all be really easy, and not require a new thread :<
http://forum.unity3d.com/threads/128034-Joystick-not-working http://answers.unity3d.com/questions/228319/-nan-nan-nan-touch-control.html
Answer by winresh · Apr 11, 2012 at 03:00 PM
can I ask you some question @gamefreak...because my joystick is not moving and i use the script of penelope and follow the step you gave in your comments..do you have any idea why i can't move the joystick.....pls ....help me dude...
You need an iPhone / android phone to yet it (either unity remote or build it) Further be sure you have attached joystick script and the third person script and assigned its bariables
I'm sorry for that, i thought i can move it with my mouse..
Is there any way to test it because i don't have yet my android. I'm just doing it for my thesis I hope you can help me...
Thanks..Thanks...Thanks... @GameFreak
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to make camera position relative to a specific target. 1 Answer
Exception error 1 Answer
3 errors in scrip. 1 Answer
what is wrong with this online FPS script(not done) ? 1 Answer