- Home /
Touch Follow 3d.
I’m struggling right now for the control of my game all I can see in the youtube and into the unity forum is touch follow of 2D. Help me I’m so depress for this control I can’t fix this. The 3d gameobject must follow my finger when I touch it in the screen.
using UnityEngine;
using System.Collections;
public class TouchMove : MonoBehaviour {
public float speed = 0.02f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0) {
// The screen has been touched so store the touch
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
// If the finger is on the screen, move the object smoothly to the touch position
Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y,10));
transform.position = Vector3.Lerp(transform.position, touchPosition, Time.deltaTime);
}
}
}
}
I see nothing wrong with this code. This code will cause your object to move towards the first finger in the touch array at the rate of 1 unit per second. What behavior are you getting, and how is it different than your desired behavior? What is your scale?
yeah it's working perfectly in 2d, but when in 3d I touch the screen my character go up. I only want x z to move my character not x y. help me T_T
Answer by robertbu · Aug 18, 2014 at 05:13 PM
Aaaah, now I see your problem. In asking questions, you really need to spell out the issue since we don't see your action. The current code is moving the object in the 3D space. I'm assuming you have an angled camera with an object that is setting on a plane parallel to the XZ plane. The solution is to use a Raycast(). I like to use Unity's mathematical Plane class to solve this problem. Here is an untested rewrite of your code to use a the Plane class to solve your problem:
using UnityEngine;
using System.Collections;
public class TouchMove : MonoBehaviour {
public float speed = 0.02f;
void Update () {
if (Input.touchCount > 0) {
// The screen has been touched so store the touch
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
// If the finger is on the screen, move the object smoothly to the touch position
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
float dist;
if (plane.Raycast (ray, out dist)) {
transform.position = ray.GetPoint (dist);
}
}
}
}
}
I LOVE YOU $$anonymous$$AN !!!! THAN$$anonymous$$S FOR THIS <3
there's a problem here man I thought it was good but when I test it and touch into the screen the character blink into that position.
Yep, the code transports to that position. You did not specify otherwise in your question. If you want to do it over time, change lines 16 to:
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, ray.GetPoint(dist), Time.deltaTime * speed);
...where 'speed' is a variable you define or it can be replaced by a constant. 'speed' is units per second. Note that visually, when the object is further from the camera, it will appear to move slower.
why my character always collide to my gameobjects even though it has a collider. :\
@$$anonymous$$aribe1 - I don't understand your comment. This question and the code has nothing to do with collisions. $$anonymous$$aybe you want to open a new question with a full description of correct behavior and what is going wrong.
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Touchinput script converted from 3d to 2d 1 Answer
Adding Touch Controls to Car Game 1 Answer
Move to touch direction 2 Answers