Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LittleCat1 · Mar 16, 2012 at 09:45 AM · errortouchjoystickcontrol

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'.

Comment
Add comment · Show 12
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GameFreak · Mar 16, 2012 at 10:04 AM 0
Share

Why dont you use the original joystick script which Unity provides with the Penelope tutorial?

http://unity3d.com/support/resources/tutorials/penelope

avatar image LittleCat1 · Mar 16, 2012 at 10:10 AM 0
Share

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

avatar image fafase · Mar 16, 2012 at 10:24 AM 0
Share

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...

avatar image GameFreak · Mar 16, 2012 at 10:30 AM 0
Share

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

avatar image LittleCat1 · Mar 16, 2012 at 10:31 AM 0
Share

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

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
     }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LittleCat1 · Mar 16, 2012 at 12:35 PM 0
Share

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 :)

avatar image LittleCat1 · Mar 16, 2012 at 03:06 PM 0
Share

Anyone? :< :<

avatar image aldonaletto · Mar 16, 2012 at 04:59 PM 0
Share

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); 
}
avatar image LittleCat1 · Mar 16, 2012 at 05:11 PM 0
Share

Joystick2 is the name of the file I couldnt get to work originally.

Thanks for being super :)

avatar image LittleCat1 · Mar 16, 2012 at 05:15 PM 0
Share

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

avatar image
0

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...

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GameFreak · Apr 11, 2012 at 05:33 PM 0
Share

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

avatar image winresh · Apr 13, 2012 at 01:02 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges