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 Spincu · Aug 01, 2013 at 12:29 PM · raycasttouchjoystickmultitouchcrosshair

Touch Crosshair problem.

I have a game in whici I use a joystick and with my other finger I can move around in an invisible rectangle on the screen an empty game object to which I have attached a crosshair.

It is working fine as long as I'm touching in that rectangle but the moment I lift my finger from that area , the object doesn't move anymore in space, because nothing is touched in that area. I need somehow to save it's position on the screen even when I lift my fingers :)

Any sugestions ?

 // public variables
 public var myobject     : GameObject;   //the empty game object that the turret and cannon wil be following
 public var range        : float = 10.0; // range of the object in the z axis
 public var moveTouchPad : Joystick;
 //private variables
 
 //public static var  TargetingArea2: Rect = new Rect(Screen.width - (Screen.width*0.9) , Screen.height*0.4, Screen.width, Screen.height/2); //tank cannot aim all the way up
 public static var  TargetingArea1: Rect = new Rect(0, Screen.height * 0.5, Screen.width, Screen.height/2); //places where the touch is read
 
 var lastpos = Vector3.zero;
 
 public var color: Color = Color.red;   
 // in this script we put the empty game object that will always be on the mouse position and is the object that our turret will be actually 
 // rotating towards
 function Start(){
         Screen.showCursor = false;            
 }
 
 function Update () {    
       
    for (var touch : Touch in Input.touches){
         
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 
              if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {
                  if(TargetingArea1.Contains(touch.position) /*|| TargetingArea2.Contains(touch.position) */){
                      //myobject.transform.position = ray.GetPoint(range)
                      myobject.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, range ));
                  
                  }
              }
              else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
              {
                      
                      if(TargetingArea1.Contains(touch.position){
                      /// i need it the crosshair to keep the same coordinates on screen even when i lift my fingers from this area
                       
                      
                      
                      }   
                     ////
              }    
     }
 }
 
Comment
Add comment · Show 1
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 Spincu · Aug 01, 2013 at 04:45 PM 0
Share

I found another aproach on the answers around here, something with finger ID's. The following code bellow works only if I first touch the joystick and then the other part of the screen, but the moment It's much more gameplay friendly without usin rectangles as you can move more freely on the screen expect for the exact surface you don't want it to go.

I take my ai$$anonymous$$g finger away it goes back on top of the joystick. It's something about their order and how they increment their ID's.

 function Guncontroll() {
      
         var ray: Ray;
         
         for (var touch : Touch in Input.touches){
       
             if(Input.touchCount > 0 && Input.GetTouch(0).fingerId != moveTouchPad.lastFingerId){
             
             myobject.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, range ));
             
             }
            
             if(Input.touchCount > 1 && Input.GetTouch(0).fingerId == moveTouchPad.lastFingerId){
             
             myobject.transform.position = Camera.main.ScreenToWorldPoint(new Vector3 (touch.position.x, touch.position.y, range ));
     
             }
         }
     }

 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jacobschellenberg · Aug 01, 2013 at 04:10 PM

Perhaps have the Crosshairs position be that of the finger position? Otherwise it seems you would always have to touch the Crosshair to move it, rather then the crosshair being where your finger is.

Comment
Add comment · 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

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

16 People are following this question.

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

Related Questions

How to properly handle multitouch for virtual joystick? 0 Answers

Android 2D multitouch joystick + buttons 0 Answers

How to stop second finger from manipulating position of gui joystick 1 Answer

Touch ID ????? 1 Answer

Get the position of the first finger that touched the collider? 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