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 /
This question was closed Feb 20, 2019 at 11:41 PM by redcloud319 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by redcloud319 · Feb 19, 2019 at 02:40 AM · touchcamera-movementtilemapplayer movementdrag

Touch Camera Movement and Transform Player on Touch

Hey All, be kind i am just learning Unity and C#. My Goal is to make a tile strategy game, in which you can scroll across a TileMap Board and then select a tile and move a player Entity to the selected tile.

i am having an issue with my touch interaction with my camera verse the selection of the tile to move the player. I am developing for Android right now so it will be a touch screen. Every time i try to just move my camera i place my player on the first tile i select when i just want to scroll ac cross the TileMap. I would like to figure out a clean way go across the map with out placing the player unless i tap a tile. I have played around with making a flag if TouchPhase.Moved =true dont place the player but it just places it anyway since the initial TouchPhase is not Moved but Began.

I feel like having competing updates in 2 scripts is a bad idea....

Camera Scroll Function

 public GameObject camera_GameObject;
     Vector2 StartPosition;
 
     // Update is called once per frame
     void Update()
     {
         if (Input.touchCount == 1)
         {
             if (Input.GetTouch(0).phase == TouchPhase.Moved)
             {
                 Debug.Log(Input.GetTouch(0).phase.ToString());
                 Vector2 NewPosition = GetWorldPosition();
                 Vector2 PositionDifference = NewPosition - StartPosition;
                 camera_GameObject.transform.Translate(-PositionDifference);
             }
             StartPosition = GetWorldPosition();
                 
         }
 
     }
 
     Vector2 GetWorldPosition()
     {
         return camera_GameObject.GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition);
     }

Player Tile Move Script ( This is Attached to my Player Object)

 public Tilemap tilemap;
 
 public void Update()
     {
         TargetPosition();
     }
 
     public void TargetPosition()
     {
         if (Input.GetMouseButtonDown(0))
             //if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) //<-- Used for Touch Screen only    
         {
             Debug.Log("Mouse DOWN");
             if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
             {
                 Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 Vector3Int target = tilemap.WorldToCell(mouseWorldPos);
                 transform.position = tilemap.GetCellCenterWorld(target);    // Move there
                 SavePlayerPosition();
                 GameControl.control.platinum += 1;
             }
             else { Debug.Log("Detected GameObject On Touch"); }
             
         }
     }



Comment
Add comment · Show 1
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 redcloud319 · Feb 20, 2019 at 11:42 PM 0
Share

Found a solution! $$anonymous$$y problem was my logic in my IF statements in the camera and the bool switch. I also had to move my action for the player into the camera script

 public class Camera2 : $$anonymous$$onoBehaviour
 {
     public Tilemap tilemap;
     public GameObject camera_GameObject;
     Vector2 StartPosition;
     private bool _moved;
     public Player player;
     // Update is called once per frame
     void Update()
     {
 
         if (Input.touchCount == 1)
         {
             if (Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved)
             {
                 Vector2 NewPosition = GetWorldPosition();
                 Vector2 PositionDifference = NewPosition - StartPosition;
                 camera_GameObject.transform.Translate(-PositionDifference);
                 _moved = true; // Remember that the player moved
             }
             StartPosition = GetWorldPosition();
                 
         }
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
         {
             if (!_moved)
             {
                TargetPosition();
             }
             _moved = false;
         }
 
     }
 
     Vector2 GetWorldPosition()
     {
         return camera_GameObject.GetComponent<Camera>().ScreenToWorldPoint(Input.mousePosition);
     }
 
     public void TargetPosition()
     {
             Debug.Log("$$anonymous$$ouse DOWN");
             if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
             {
                 Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 Vector3Int target = tilemap.WorldToCell(mouseWorldPos);
                 player.transform.position = tilemap.GetCellCenterWorld(target);    // $$anonymous$$ove there
                 SavePlayerPosition();
                 GameControl.control.platinum += 1;
             }
             else { Debug.Log("Detected GameObject On Touch"); }
 
     }
     public void SavePlayerPosition()
     {
         GameControl.control.PlayerX = transform.position.x;
         GameControl.control.PlayerY = transform.position.y;
         GameControl.control.PlayerZ = transform.position.z;
     }
 
 
 
 
 }

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

204 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player won't stay on my tile map 0 Answers

Cant figure out simple camera orbit 0 Answers

How to have your player controls change while your camera rotates? 0 Answers

Is it possible to detect touch In a certain GUI element? 1 Answer

Locking Camera's rotation on a rolling character 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