- Home /
How to drag an object along a path using touch controls in specific areas?
Hello I'm new to unity and coding in general. I am more of an artist then a programer but I'm giving it a shot anyway. Also I'm new to the questions board so if I got this in the wrong place, I'll try to fix it.
My problem is that I want to touch and drag anywhere on one side of the screen, to move a specific object up and down along a path. Wile rotating the object along the way. And then do the same on the other side of the screen. I've looked for tutorials but can't seem to find anything. Also I'm coding for android in the Unity 2d aspect if that makes a difference.
I made an illustration showing what I'm after.
Any explanations or links to tutorials on the subject would be helpful. Thank you for reading.
Some clarity on the question.
The character stays stationary in the center of the screen wile bullets fly at him/her. There are two shields that rotate around the character in an ark but don't go all the way around the character. There are menus at the top and bottom of the screen that define the area that bullets fly.
current layout of game so far.
The character is moved along the path by the finger but the shield is also rotated. Correct?
Does the finger rotate the shield, or does the shield follow the angle of the finger with respect to the character?
Can a shield rotate beyond its side of the character?
What defines the path the object is following?
If the finger moves the character, what happens with two fingers going in different directions, or two fingers going in the same direction at different speeds?
I assume the camera is following the character to keep him centered?
Updated the main post to reflect more data on whats going on.
Answer by robertbu · Aug 13, 2014 at 11:45 PM
Here is a bit code that solves it for a mouse. I leave it up to you to convert it to touch. To work, you have to set things up in a particular way.
Create an empty game object a the center of the ship. Name it 'Right'
Place the right shield directly to the right of the ship at the correct distance
Make the shield a child of the empty 'Right' game object
Attach the script below to the empty parent named 'Right'
Duplicate the 'Right' game object which will also duplicate the child shield. Name the new parent object 'left'
Select the game object now named 'Left' and uncheck the 'isRight' variable in the Inspector
Set the rotation for the 'Left' game object to 180.0
Hit play, and hold the mouse button down to move the shields.
Note I added a bit of easing with respect to rotation. That way when a finger is first pressed down, or a mouse is first clicked, the shield does not immediately snap to the angle, but visibly rotates to the angle. If you don't what that, directly assign the result of the AngleAxis() call to transform.rotation. Also if you did not get it from the steps above, if the parent empty game object for each shield has rotation (0,0,0), then the shield for both would be on the right.
#pragma strict
public var isRight : boolean = true;
public var speed = 180.0;
function Update () {
if (Input.GetMouseButton(0)) {
var onRight = (Input.mousePosition.x > Screen.width / 2);
if (isRight && onRight || !isRight && !onRight) {
var pos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - pos;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
var targetRot = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, Time.deltaTime * speed);
}
}
}
P.S. I just took a closer look at your diagram. Given the arc you are looking for, the empty 'Left' and 'Right' game objects need to be places at the center of the circle formed by that arc, not at the center of the ship.
Thank you so much this is exactly what I was looking for! I don't know anything about java. I've been learning c# as my first progra$$anonymous$$g language. $$anonymous$$aybe that's why I couldn't find any tutorials that had something I could use. Anyway, off to do research on how to make this work for touch controls.
Here is a C# conversion of the script if you are working in C#:
using UnityEngine;
using System.Collections;
public class Shields2 : $$anonymous$$onoBehaviour {
public bool isRight = true;
public float speed = 270.0f;
void Update () {
if (Input.Get$$anonymous$$ouseButton(0)) {
bool onRight = (Input.mousePosition.x > Screen.width / 2.0f);
if (isRight && onRight || !isRight && !onRight) {
Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - pos;
float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
Quaternion targetRot = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, Time.deltaTime * speed);
}
}
}
}
I can't seem to get the c# code to work. the java code works fine but when I make a new script for the c# and attach it to the blank object then run the game. It gives me an error.
The referenced script on this Behaviour is missing!
Not sure what the problem is.
Not sure. You did remove the original code from the game object? I did test the code before posting it, and it worked fine for me.
Your answer
Follow this Question
Related Questions
Android internal storage folder 0 Answers
Create iTextSharp PDF in Android 1 Answer
How to drag object along with dragging touch? 1 Answer
Finger gesture works/fails on different devices? 0 Answers
Android file path? 0 Answers