- Home /
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 .
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 ) ;
}
}
}
What do you mean "It didn't work", be specific if you want somebody to help you.
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.
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 .
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);
}
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
Checkout the Lerp. Documentation. ( you don't need raycast to get that behaviour. do you want me to update the answer? )
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 ^^
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.