Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by RoseRei · Aug 12, 2014 at 08:40 PM · androidpathfindingdragpathtouch controls

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.

alt text

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.

alt text

touch-controls-exsplanation.jpg (87.8 kB)
layoutfullsmall.jpg (140.4 kB)
Comment
Add comment · Show 2
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 robertbu · Aug 12, 2014 at 09:00 PM 0
Share
  • 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?

avatar image RoseRei · Aug 13, 2014 at 10:30 PM 0
Share

Updated the main post to reflect more data on whats going on.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

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 RoseRei · Aug 14, 2014 at 01:18 AM 0
Share

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.

avatar image robertbu · Aug 14, 2014 at 01:19 AM 1
Share

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); 
             }
         }
     }
 }

avatar image RoseRei · Aug 14, 2014 at 06:29 AM 0
Share

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.

avatar image robertbu · Aug 14, 2014 at 06:43 AM 1
Share

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

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

2 People are following this question.

avatar image avatar image

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


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