Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by ecequalsm2 · Feb 03, 2016 at 10:28 PM · canvasdragtouch controlstouchpaddrag objects

Trying to drag GameObject using touch input

I'm trying to combine 2 scripts to get my player to follow the movement of my finger across the screen. I was trying to combine this awesome YouTube tutorial that I like for movement with the 'MOBILE DEVELOPMENT: CONVERTING SPACE SHOOTER TO MOBILE' Unity tutorial video for creating a touch pad and making my player follow my finger. But my PlayerController script is different and I'm having trouble combining the two. So far, I can get the Debug.Log to provide a readout of where my finger is, but for some reason, I can't get the ship to actually follow the same position as my finger's current position in screen space(I'm pretty much stuck at the 52 minute point in the video, where the original movement script is readjusted for touch input based on what's in the SimpleTouchPad script. Can anyone help me?

Thanks in advance, Millie

Here's Unity's script:

using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using System.Collections;

public class SimpleTouchPad : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler {

 private Vector2 origin;
 private Vector2 location;

 void Awake()
 {
     location = Vector2.zero;
 }

 public void OnPointerDown(PointerEventData data)
 {
     origin = data.position;
 }

 public void OnDrag(PointerEventData data)
 {
     Vector2 currentPosition = data.position;
     Vector2 directionRaw = currentPosition - origin;
     Vector2 location = directionRaw.normalized;
     Debug.Log(location);
 }
 public void OnPointerUp(PointerEventData data)
 {
     location = Vector2.zero;
 }

 public Vector2 GetDirection()
 {
     return location;
 }

}

And here's my script (trying to implement the change required):

using UnityEngine; using UnityEngine.UI; using System.Collections;

public class PlayerControl : MonoBehaviour {

 public GameObject GameManagerGO;

 public GameObject PlayerBullet1GO;
 public GameObject bulletPosition01;
 public GameObject bulletPosition02;
 public GameObject ExplosionGO; //explosion prefab

 public float fireRate = 0.5f;

 public float speed = 3f;

 public SimpleTouchPad touchPad;

 // Use this for initialization
 void Start()
 {

     InvokeRepeating("Shoot", 2f, fireRate);
 }

 // Update is called once per frame
 void Update()
 {

     //web version

     /*
      float x = Input.GetAxisRaw("Horizontal"); //-1 = left, 1 = right
      float y = Input.GetAxisRaw("Vertical"); //-1 = down, 1 = up
        
    
 */
     //compute a direction vector based on the input and normalize it to get a unit vector
     Vector2 direction = new Vector2(x, y).normalized;      
      

     //Trying to use touchPad from SimpleTouchPad script to influence movement, but it won't work
     Vector2 location = touchPad.GetDirection();
     //This was the part in the tutorial that was readjusted to reflect the Vector2 value set in     
     //SimpleTouchPad
     Vector2 movement = new Vector2(location.x, location.y);
 

       Move(direction);
 }

 void Move(Vector2 direction)
 {
     //Find the screen limits to player's movement(left, right, top and bottom edge
     //of the screen)
     Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); //bootom-left corner)
     Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); //top-right corner;

     max.x = max.x - 0.225f; //subtract the player Sprite half width
     min.x = min.x + 0.225f; //add player sprite half width;

     max.y = max.y - 0.285f; //subtract player sprite half height
     min.y = min.y + 0.285f; //add player sprite half height


     //Get player's current position
     Vector2 pos = transform.position;

     //Calculate the new position
     pos += direction * speed * Time.deltaTime;

     //Make sure the new position is not outside the screen
     pos.x = Mathf.Clamp(pos.x, min.x, max.x);
     pos.y = Mathf.Clamp(pos.y, min.y, max.y);    

     //Update the player's position
     transform.position = pos;
 }

 //Function to make player shoot
 public void Shoot()
 {
     //play the laser sound effect
 //    GetComponent<AudioSource>().Play();

     //Instantiate the first bullet
     GameObject bullet01 = (GameObject)Instantiate(PlayerBullet1GO);
     bullet01.transform.position = bulletPosition01.transform.position; //set bullet's initial position


     //Instantiate the second bullet
     GameObject bullet02 = (GameObject)Instantiate(PlayerBullet1GO);
     bullet02.transform.position = bulletPosition02.transform.position; //set bullet's initial position
 }

}

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Input.mousePosition doesnt work 0 Answers

Unity UI does not visible when i drag using canvas set to screen space - camera 1 Answer

How to move a gameobject from exactly where it was touch 0 Answers

Drag and Stay(Fly) 0 Answers

Positioning object on mouse position 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