Hi all,
I have done on touch to move object anywhere on screen. But my requirement is to just touch particular object and move on. Any help please ??
This is my code:
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
Vector2 touchdeltaposition = Input.GetTouch (0).deltaPosition;
anglex = (float)(touchdeltaposition.x * Time.deltaTime * 10.0);
angley = (float)(touchdeltaposition.y * Time.deltaTime * 10.0);
elevationAngle = new Vector3 (anglex, angley, 0);
Vector3 elevation = Quaternion.Euler (elevationAngle) * transform.forward;
rigidbody2D.AddForce (elevation/2);
rigidbody2D.velocity = new Vector2 (anglex, angley);
}
In my 2d Platformer (Unity 4.3) I had 3 scripts for left & right movement. One was Player.cs (Player Raycasting, Left and right movement, etc), MoveLeft.cs and Move Right.cs.
With Player.cs I had a setup Like this (Attached to Player)
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public float PlayerMoveSpeed = 3f;
public static bool PlayerMoveRight = false;
public static bool PlayerMoveLeft = false;
// Use this for initialization
void Update()
{
//Run these to functions
Movement();
}
void Movement()
{
//Touch Controls
if(MoveRight == true)
{
transform.Translate(Vector2.right * PlayerMoveSpeed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
//Left Movement triggered
if(MoveLeft == true)
{
transform.Translate(Vector2.right * PlayerMoveSpeed * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 180);
}
}
}
The variables MoveLeft & MoveRight would be set to true/false with these scripts…
MoveLeft.cs… For Left movement on touch (Attach to guiTexture Called MoveLeft)
using UnityEngine;
using System.Collections;
public class MoveLeft : MonoBehaviour {
// Update is called once per frame
void Update ()
{
//Is there a touch?
if(Input.touches.Length <= 0)
{
//If no touches...
}
else //If screen touched..
{
//Loop through these
for(int i = 0; i < Input.touchCount; i++)
{
//executes this code for current touch
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
//If current touch hits the guitexture...
//MoveRight bool within PlayerLogic = false when touch has Began if(Input.GetTouch(i).phase == TouchPhase.Began)
{
Player.PlayerMoveLeft = true;
}
//MoveLeft bool within PlayerLogic = false when touch has ended if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
Player.PlayerMoveLeft = false;
}
}
}
}
}
}
MoveRight.cs… For Left movement on touch (Attach to guiTexture Called MoveRight)
using UnityEngine;
using System.Collections;
public class MoveRight : MonoBehaviour {
// Update is called once per frame
void Update ()
{
//Is there a touch?
if(Input.touches.Length <= 0)
{
//If no touches...
}
else //If screen touched..
{
//Loop through these
for(int i = 0; i < Input.touchCount; i++)
{
//executes this code for current touch
if(this.guiTexture.HitTest(Input.GetTouch(i).position))
{
//If current touch hits the guitexture...
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
Player.PlayerMoveRight = true;
}
if(Input.GetTouch(i).phase == TouchPhase.Ended)
{
Player.PlayerMoveRight = false;
}
}
}
}
}
}
Change script names if needed and If you’re using Javascript Google c# to javascript online and click on the first result.