Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 SkyFrostDev · Apr 08, 2021 at 06:40 AM · scripting problembuttonstouch controlsmobile devicestouch screen

Grabbing objects for touch controls? how

Hi I need help for unity adding buttons that if you press it, it will grab that object and it will stay on players hand my player is a stickman ragdoll theirs a picture of how it looks like it only have 1 arm it doesnt have MHMHmh doesnt have elbow just a stick that attached to the torso and I need it to grab objects with it like a sword, just a sword or guns etc not the enemies that he can grab only the swords etc also if you uhmmm click the button again it will unattach to the players hand idk Any coding :D pls help I would reward you with reputation points for sure also pls add if you touch it with mouse pointer it would work so I can actually try it out on pc while its not published yet :D BUTTONS BUTTONS BUTTON BUTTON CLICK CLICK, FOR MOBILE MOBILE PHONES PHONESalt text

rere.png (221.8 kB)
Comment
Add comment · Show 3
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 SkyFrostDev · Apr 08, 2021 at 06:41 AM 0
Share

btw im asking this coz I cant see any tutorials of it I only saw on youtube but its for keyboard controls but I need it for touch controls D: pls help thanks! im a frikin noob sorry

avatar image VoidVenom SkyFrostDev · Apr 08, 2021 at 06:48 AM 0
Share

Hey man, no need to apologize for being a "noob"! We all started somewhere!

avatar image SkyFrostDev VoidVenom · Apr 08, 2021 at 06:52 AM 0
Share

oh, thanks man :D really appreciate that, lol still im a noob

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by VoidVenom · Apr 08, 2021 at 06:47 AM

You can use Input.GetTouch to get a Touch struct. One of the parts of this struct is the screen-space coordinate that was touched. You'll need a bit of extra code (see the linked documentation) for tracking specific fingers and so on, but that's the basics. You can then use Camera.ScreenToWorldPos to shoot a raycast into the world and see if the touch hit the hand. From there, it's just like dragging with a mouse - track the position to the touch until it is released, then stop tracking the position to the touch.

I forgot to mention about touch controls on desktop temporarily.. you should be able to just add code that would work on desktop and have it work on both.

EDIT: Added potential code solution as requested by question author.

 using UnityEngine;

 public class TouchTracking : MonoBehaviour {

     [SerializeField] private Transform target;
     [SerializeField] private Camera viewport;
     [SerializeField] private bool useMouse = true;

     // these keep track of important tracking info, such as finger and whether we're tracking or not
     private int trackingFinger = 0;
     private bool track = false;

     void Awake() {
         // if you leave the viewport field blank in the Inspector, 
         // use the first camera in your scene with the "MainCamera" tag.
         if (viewport == null)
             viewport = Camera.main;
     }

     void Update() {
         /**
         * Touch
         */
         int touchCount = Input.touchCount; // this tells you how many fingers are on the screen right now
         Touch touch; // this stores information about the touch
         for (int id = 0; id < touchCount; id++) {
             touch = Input.GetTouch(id);
             if (touch.phase == TouchPhase.Began && CheckCollision(touch.position))
                 track = true;
             else if (touch.phase == TouchPhase.Ended)
                 track = false;
         }

         /**
         * Mouse
         */
         // button 0 is left mouse
         if (Input.GetMouseButtonDown(0) && CheckCollision(Input.mousePosition))
             track = true;
         else if (Input.GetMouseButtonUp(0))
             track = false;
         /**
         * Perform the tracking operation if needed
         */
         if (track) {
             Vector3 inputPos;
             if (useMouse)
                 inputPos = Input.mousePosition;
             else
                 inputPos = Input.GetTouch(trackingFinger).position;

             Ray r = viewport.ScreenPointToRay(inputPos);
             float distance = (target.position - r.origin).magnitude;
             MoveToTouch(target, r.GetPoint(distance));
         }
     }

     bool CheckCollision(Vector3 position) {
         Ray ray = viewport.ScreenPointToRay(position);
         Vector3 worldPoint = ray.origin;
         Vector3 worldDirection = ray.direction;

         RaycastHit hit; // stores information about the raycast hit (if there was one)
         if (Physics.Raycast(worldPoint, worldDirection, out hit, Mathf.Infinity))
             return hit.collider.gameObject == target.gameObject;
         else return false;
     }

     void MoveToTouch(Transform target, Vector2 position) {
         float lerpedX = Mathf.Lerp(target.position.x, position.x, 100 * Time.deltaTime);
         float lerpedY = Mathf.Lerp(target.position.y, position.y, 100 * Time.deltaTime);

         target.position = new Vector3(lerpedX, lerpedY, target.position.z);
     }
 }



Comment
Add comment · Show 27 · 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 SkyFrostDev · Apr 08, 2021 at 06:51 AM 0
Share

Can you make one for me pls pls pls I dont have any idea bout scripting Even if im copying one still getting frikin errors dude xD Ill add reputation for you if you do so :D plsp lspls D:

avatar image VoidVenom SkyFrostDev · Apr 08, 2021 at 06:53 AM 0
Share

Sure thing, I'll need a few $$anonymous$$utes though :)

avatar image SkyFrostDev VoidVenom · Apr 08, 2021 at 06:54 AM 0
Share

holy mother of god! thanks man D:

Show more comments
avatar image
0

Answer by Nistroy · Apr 09, 2021 at 02:18 PM

you can use Lean, which can be installed for free in the AssetStore https://assetstore.unity.com/packages/tools/input-management/lean-touch-30111

Comment
Add comment · Show 1 · 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 SkyFrostDev · Apr 13, 2021 at 07:21 AM 1
Share

thanks ill try it :))

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

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

Mobile Joystick doesn't work when Mouse Cursor is locked 0 Answers

Is there a way to disable something permanently in a script? 2 Answers

How to create a lasting UI button that would set a game object to active . 0 Answers

Touch input problem 1 Answer

How do I connect my jump button to my player script? 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