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 Dogg · Sep 11, 2014 at 10:18 PM · c#iostouch

Multi Touch Drag Screen To Move Player?

Hello. it's been a little while since I've last ask a question on here. I've been busy. Anyways I was messing around with touch controls and i was wondering if it's possible to have multi touch controls, to where if I drag my finger to the left, the Player moves left, and if I drag it to the right, the Player moves right. I've seen people directly grab the object and drag it, but I don't want to do that. I want to just drag the screen for the Player to move, and if I stop the Player will stop.

Right now my Player movements are set to the default arrow keys, so think of it as that. I also have a jump control to where you tap on any part of the screen and the Player jumps, so that's why this needs to be multi touch or else it will affect the jump controls. By the way i'm testing controls so nothing is finalized yet, so feel free to suggest different ways of moving my Player. One way I already tried was virtual Joystick movement, I haven't got it working yet. Anyways thanks for reading i hope you guys/gals can help.

Here's a little part of my Character Controller script showing some things related to movement and jump:

 public float defaultSpeed = 5f;
 public float maxSpeed = 10f;
 public float speed = 5f;
 
 bool grounded = false;
 public Transform groundCheck;
 float groundRadius = 0.2f;
 public LayerMask whatIsGround;
 public float jumpForce = 700f;
 
 
 void FixedUpdate ()
     {
         
         
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
         anim.SetBool ("Ground", grounded);
         
         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
         
         
         if (!grounded)
             return;
 
         float move = Input.GetAxis ("Horizontal");
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
         
         if (move > 0 && !facingRight)
             Flip ();
         else if (move < 0 && facingRight)
             Flip ();
         
     }
     
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     void Update()
     {
         if (grounded && Input.GetMouseButtonDown(0)) {
             anim.SetBool ("Ground", false);
             rigidbody2D.AddForce (new Vector2 (0, jumpForce));
             AudioSource.PlayClipAtPoint(JumpAudio[ Random.Range(0, JumpAudio.Length) ], transform.position, .6f);
         }
     }
 }
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 Dogg · Sep 14, 2014 at 01:07 AM 0
Share

Anyone else have any ideas? This topic seems to be pretty vague.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by orb · Sep 12, 2014 at 08:53 AM

Since you're saying "multi touch" I'm assuming you mean two or more fingers.

What you're looking for first then is Input.touchCount, which tells you how many fingers are currently touching the screen. Input.GetTouch(0) and Input.GetTouch(1) will give you the first two, and the deltaPosition property on each will give you the amount of movement. You can check Input.GetTouch(0).phase for the TouchPhase.Moved enum to verify that it actually HAS moved.

So a change you need to do to regular controls is to wrap it in a test for Input.touchCount == 1, for instance. Or a switch. Supporting three-finger special movement may also be useful.

With the new game controller support in iOS I think the second joystick on a typical gamepad would be the third and fourth joystick axis in the input options, but this is something I haven't had the opportunity to play with.

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 Dogg · Sep 12, 2014 at 07:47 PM 0
Share

Well I said multi touch because my jump happens when I touch any part of the screen. So I want to move the Player and be able to jump at the same time.

avatar image Dogg · Sep 12, 2014 at 09:25 PM 0
Share

Now that I think about it, the virtual joystick really would be the best option for me. The problem is, is that I can't get the joystick to only move my Player left and right. Do you know how I can achieve this? Here's my controller that controls the virtual joystick:

 public class Controller : $$anonymous$$onoBehaviour
 {
     public CNJoystick movementJoystick;
 
     private Transform transformCache;
     // Use this for initialization
     void Awake()
     {
         if (movementJoystick == null)
         {
             throw new UnassignedReferenceException("Please specify movement Joystick object");
         }
         movementJoystick.FingerTouchedEvent += Start$$anonymous$$oving;
         movementJoystick.FingerLiftedEvent += Stop$$anonymous$$oving;
         movementJoystick.Joystick$$anonymous$$ovedEvent += $$anonymous$$ove;
 
         transformCache = transform;
     }
 
     // You can extend this class and override any of these virtual methods
     protected virtual void $$anonymous$$ove(Vector3 relativeVector)
     {
         // It's actually 2D vector
         transformCache.position = transformCache.position + relativeVector;
         Face$$anonymous$$ovementDirection(relativeVector);
     }
 
     private void Face$$anonymous$$ovementDirection(Vector3 direction)
     {
         if (direction.sqr$$anonymous$$agnitude > 0.1)
         {
             transform.up = direction;
         }
     }
 
     protected virtual void Stop$$anonymous$$oving()
     {
         
     }
 
     protected virtual void Start$$anonymous$$oving()
     {
 
     }
 
 }

The transformCache.position = transformCache.position + relativeVector; is what moves my Player.

avatar image orb · Sep 14, 2014 at 11:57 AM 0
Share

relativeVector is an x and y coordinate, so it would move left, right, up and down. If you simple refactor that to include just a float parameter for horizontal movement and making a new Vector2(horizontal, 0f) it'll stick to left/right.

avatar image Dogg · Sep 14, 2014 at 11:13 PM 0
Share

Sorry, but I'm not sure how to do that. I tried this:

 float horizontal = 0f;
 
    
     // You can extend this class and override any of these virtual methods
     protected virtual void $$anonymous$$ove(Vector2 horizontal)
     {
         // It's actually 2D vector
         transformCache.position = transformCache.position + horizontal;
         Face$$anonymous$$ovementDirection(horizontal);
     }
 
     private void Face$$anonymous$$ovementDirection(Vector2 direction)
     {
         if (direction.sqr$$anonymous$$agnitude > 0.1)
         {
             transform.up = direction;
         }

But of course that doesn't work and gives me errors.

avatar image orb · Sep 15, 2014 at 11:06 AM 0
Share

$$anonymous$$ore like this:

 protected virtual void $$anonymous$$ove(Vector3 relativeVector)
 {
     Vecto2 pos = new Vector2(transformCache.position.x + relativeVector.x, transformCache.position.y);
     transformCache.position = pos;
     Face$$anonymous$$ovementDirection(pos);
 }
 

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Ride across a tree? 1 Answer

Detect the swipe and add force respective to it? 3 Answers

What is the best way to understand code in a project with null documentation? 2 Answers


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