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 J-F · Mar 29, 2014 at 05:02 PM · 2dtouchaimaiming

Aim at touch (2D).

What im trying to achieve is a script that aims the gameobject it's attached to towards touch position. I have no problem doing this for standalone with the "lookAt" thingy, And i've tried to convert a touch posision to something that works with the lookAt with no succes.

I also tried using a mesh cursor where the gameobject is aimed at the cursor instead of touch, But this brought up some issues since it has to work over a network view.

What i tried was:

 using UnityEngine;
 using System.Collections;
 
 public class Controller : MonoBehaviour {
 
     void Update ()
     {
         int fingerCount = 0;
         foreach (Touch touch in Input.touches) {
             if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
                 fingerCount++;
         if (fingerCount > 0){
         transform.LookAt(Input.GetTouch(0).position);
         }
     }
 }
 }

But the position of the touch seems to be a little offset. (maybe due to the perspective and the depth position of the touch.)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ava4414 · Mar 29, 2014 at 06:02 PM

your problem could be this line:

   transform.LookAt(Input.GetTouch(0).position);

you can solve it with:

   Vector3 worldPoint = Camera.main.ScreenToWorldPoint(touch.position);
   Vector2 touchPos = Vector2(worldPoint .x, worldPoint .y);
   transform.LookAt(touchPos);

this code will convert the screen touch position to the world position that its the one you need so it will solve the problem

Comment
Add comment · Show 4 · 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 J-F · Mar 29, 2014 at 06:59 PM 0
Share

it gives me this error: (17,52): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

Do i have to create variables for X and Y?

avatar image J-F · Mar 29, 2014 at 07:31 PM 0
Share

Never$$anonymous$$d. The vector 2 on your second line should be "new Vector2"

avatar image J-F · Mar 29, 2014 at 07:43 PM 0
Share

I just tested it on a mobile device and there is something wrong with it. The object snaps into a certain rotation once you touch the screen and wont move again.

It appears that the Y axis of the object is either ai$$anonymous$$g towards the camera or somewhere very far from it.

EDIT: I forgot to change my camera to ortographic. It works fine now.

avatar image Shujahat-Murtaza · Jan 18, 2017 at 05:17 PM 0
Share

Thanks.. :)

avatar image
0

Answer by TinyRodent · Apr 09, 2019 at 06:04 AM

super late but im trying to achieve a similar goal to you and have a gameobject look at the users touch location. I cannot find any sources that have really helped but this has been the closest. The issue is though that when i do click on the screen the object looks the opposite way and even changes dimensions depending on the location of the click??!! I have followed your code to the T and cannot replicate the same results.

Hoping someone can help me out here.,super late but im trying to achieve a similar goal to you and have a gameobject look at the users touch location. I cannot find any sources that have really helped but this has been the closest. The issue is though that when i do click on the screen the object looks the opposite way and even changes dimensions depending on the location of the click??!! I have followed your code to the T and cannot replicate the same results.

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

22 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

Related Questions

Problem with gun rotation after flipping character (2D) 1 Answer

Shooting around defense position without contact 1 Answer

Touch position is off screen. 1 Answer

weapon aim when shooting 0 Answers

Drag object by Touch quickly 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