Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 marksm4nn · Aug 10, 2015 at 09:28 AM · javascripttouchtouchscreentouch screen

Move my character where i touched

I had problems taking position of my touchplace , i'm getting Index out of bounds error and i cant get any movements , i'm new , help me please. Here's the script ;

 #pragma strict
 var amirpos : Vector3 ;
 var touchpos : Vector3 ;
 var parmak : Touch ;
 var camerapos : Vector3 ;
 var touchposcam : Vector3 ;
 var speed : float = 15.0f ;
 function Start () {
 
 }
 
 function Update () 
 {
 amirpos = GameObject.Find("Amirr").transform.position;
 camerapos = Camera.main.gameObject.transform.position;
 touchposcam = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
 touchpos = Vector2 ( touchposcam.x , touchposcam.y );
 if(parmak.phase == TouchPhase.Began && Input.touchCount >0)
 {
 
 if(amirpos.x != touchpos.x)
 {
  transform.position = Vector3( (touchpos.x-amirpos.x)/speed , (touchpos.y-amirpos.y)/speed , camerapos.z-amirpos.z ) ;
 }
 }
 }


This code contains a lot of mistakes , i just added it for you to see what exactly i need . The object amirr needs to move the place where i touch and i need a script for it .

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

Answer by Hellium · Aug 10, 2015 at 09:40 AM

Even if I am not familiar with mobile games, I would have tried as follow :

 #pragma strict
 
 // Drag & drop the Amiir GameObject here
 var amiir : Transform ;
 
 // Drag & drop the main camera here
 var mainCamera : Camera ;
 
 var speed : float = 15.0f ;
 
 var amirpos : Vector3 ;
 var touchpos : Vector3 ;
 var parmak : Touch ;
 var touchposcam : Vector3 ;
 
 function Update () 
 {
     if( Input.touchCount > 0 )
     {        
         touchposcam =   mainCamera.ScreenToWorldPoint(Input.GetTouch(0).position);
         touchpos =      Vector2 ( touchposcam.x , touchposcam.y );
     }
     
     amirpos = amiir.position;

     if(parmak.phase == TouchPhase.Began )
     {
         if(amirpos.x != touchpos.x)
         {
             transform.position = Vector3( (touchpos.x-amirpos.x)/speed , (touchpos.y-amirpos.y)/speed , mainCamera.transform.position.z-amirpos.z ) ;
         }
     }
 }
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 marksm4nn · Aug 10, 2015 at 10:55 AM 0
Share

This actually didnt work , thank you anyway

avatar image Hellium · Aug 10, 2015 at 11:04 AM 0
Share

What do you mean "It didn't work", be specific if you want somebody to help you.

avatar image allenallenallen · Aug 10, 2015 at 11:45 AM 0
Share

@marksm4nn,

Don't make this difficult now. If you're here just to get a free script without understanding what it does, then don't bother; because no one will help you.

avatar image marksm4nn · Aug 10, 2015 at 03:25 PM 0
Share

I'm not here for a free script , he send me a script and i tried it , it didnt work . And i told it there are no errors but this script didnt do what i want to do and i couldnt find where's the mistake .

avatar image
0

Answer by P3JX · Aug 10, 2015 at 02:31 PM

Im not JS person. In c# This code might help

Inside the Update()

 //when you tap your screen each touch will go through this loop
 foreach (var touch in Input.touches) {
     var myTouchPosition = touch.position //this gives your touch position
     yourGameObject.transform.position = myTouchPosition; //make sense?
 
 }

  • yourGameObject = game object that you want to position

  • myTouchPosition = where you touch on the screen

Updated

Smooth transaction between touch position and game object's position

 //this store your touch position
 Vector3 _touchPosition;
 //time
 float _time = 0;
 
 void Update(){
     foreach (var touch in Input.touches) {
         //get users touch position
         var _touchPosition = touch.position;  
         //reset the time
         _time = 0;
     }
 
    _time += Time.deltaTime;
 
    //smoothly transform your game object to _touchPosition
    yourGameObject.transform.position = Vector3.Lerp(yourGameObject.transform.position, _touchPosition, _time);
 
 }

Comment
Add comment · Show 6 · 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 marksm4nn · Aug 10, 2015 at 03:28 PM 0
Share

This is kinda teleportation , i'm triying to make a smooth movement . Thank you for help , i searched for it and find a solution with raycast function but couldnt actually understand it , if anybody can do and explain it , it would be perfect

avatar image P3JX · Aug 11, 2015 at 02:29 AM 0
Share

Checkout the Lerp. Documentation. ( you don't need raycast to get that behaviour. do you want me to update the answer? )

avatar image marksm4nn · Aug 11, 2015 at 07:41 AM 0
Share

I'll be very happy if you update the answer ^^ I looked at lerp. documentation but ı'm from turkey and i'm not very good at english language . It's very hard for me ^^

avatar image P3JX · Aug 11, 2015 at 09:24 AM 0
Share

Update the answer. Basicly get your touch position. move the gameobject slowly. this video also help you to understand lerp position. this is the far i can help you with.

avatar image marksm4nn · Aug 11, 2015 at 04:01 PM 0
Share

Thank you very much.. You're awesome! ^^

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to get position of touch on touch screen 1 Answer

How can I make a GUI button be touch activated? 0 Answers

I need touch input example 1 Answer

Unity 2D Touch Controll via on screen button for Mobile 0 Answers

,How Do get Touch input to work with the Player Input Component 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