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 Performator · May 01, 2018 at 10:21 AM · move an objectclick objectsclicktomovelocationinfo

Working with moving and mouse touch to set coordinates

I'm currently trying to make a 2D game for mobile that works like this:

there are 4 squares in the middle of the screen, the player starts inside one of the squares's middle, the player can only move on the X and Y axis, once the player touches a square(that is not diagonal to the player) the player has to move to it and is not allowed to change direction until he gets inside of the middle.

This was the code I was using, the problem is that the player's character goes to wherever the mouse is in the squares, not on touch, so even if i put on diagonal he goes there. Not to mention he can change direction even if he is not in the middle of the square

TR=top right square Tl=top Left square BR=bottom Right Bl=Bottom left

INTL=inside tl INTR=inside tr INBL=inside bl INBR=inside br

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class moving : MonoBehaviour {
     public float Speed = 5.0f;
     public bool INTR = true;
     public bool INTL =true;
     public bool INBR =true;
     public bool INBL =true;
 
     public bool TR = false;
     public bool TL =false;
     public bool BL =false;
     public bool BR =false;
     public int timerTR=0;
     public int timerTL=0;
     public int timerBR=0;
     public int timerBL=0;
 
     void Start () {
 
     }
     // Update is called once per frame
     void Update () {
 
 
         CastRay ();
         MoveTowardsTarget ();
 
         if (Input.GetMouseButtonDown (0)) {
             Debug.Log ("Pressed left click, casting ray.");
 
         } 
             
 
 
 
     }
     private void MoveTowardsTarget ()
     {
     }
 
 
 
 
     void CastRay() {
 
         float speed = 1;
         Vector3 targetPosition = new Vector3 (-1.5f, 0, 0);
         Vector3 targetPosition2 = new Vector3 (1.5f, 0, 0);
         Vector3 targetPositiony = new Vector3 (1.5f, -4.5f, 0);
         Vector3 targetPositiony2 = new Vector3 (-1.5f, -4.5f, 0);
         Vector3 currentPosition = this.transform.position;
 
 
 
         if (Vector3.Distance (currentPosition, targetPositiony2) > .1f && INBL == false && BL == true && TR == false && INTR == false) { 
             Vector3 directionOfTravel = targetPositiony2 - currentPosition;
             directionOfTravel.Normalize ();
 
             this.transform.Translate (
                 (directionOfTravel.x * Speed * Time.deltaTime),
                 (directionOfTravel.y * Speed * Time.deltaTime),
                 (directionOfTravel.z * Speed * Time.deltaTime),
                 Space.World);
 
 
             INBL = true;
 
 
             INTL = false;
             INTR = false;
             INBR = false;
         }
 
 
 
         if (Vector3.Distance (currentPosition, targetPositiony) > .1f && BR == true && INBR == false && TL == false && INTL == false) { 
             Vector3 directionOfTravel = targetPositiony - currentPosition;
             directionOfTravel.Normalize ();
 
             this.transform.Translate (
                 (directionOfTravel.x * Speed * Time.deltaTime),
                 (directionOfTravel.y * Speed * Time.deltaTime),
                 (directionOfTravel.z * Speed * Time.deltaTime),
                 Space.World);
 
 
 
             INBR = true;
 
 
 
             INBL = false;
             INTL = false;
             INTR = false;
 
 
         }
 
         if (Vector3.Distance (currentPosition, targetPosition) > .1f && TL == true && INTL == false && BR == false && INBR == false) { 
             Vector3 directionOfTravel = targetPosition - currentPosition;
             directionOfTravel.Normalize ();
 
             this.transform.Translate (
                 (directionOfTravel.x * Speed * Time.deltaTime),
                 (directionOfTravel.y * Speed * Time.deltaTime),
                 (directionOfTravel.z * Speed * Time.deltaTime),
                 Space.World);
 
 
             INTL = true;
 
             INTR = false;
             INBL = false;
             INBR = false;
         }
 
 
 
 
 
 
         if (Vector3.Distance (currentPosition, targetPosition2) > .1f && TR == true && INTR == false && BL == false && INBL == false) { 
             Vector3 directionOfTravel = targetPosition2 - currentPosition;
             directionOfTravel.Normalize ();
 
 
             this.transform.Translate (
                 (directionOfTravel.x * Speed * Time.deltaTime),
                 (directionOfTravel.y * Speed * Time.deltaTime),
                 (directionOfTravel.z * Speed * Time.deltaTime),
                 Space.World);
 
             INTR = true;
 
 
             INBL = false;
             INBR = false;
             INTL = false;
         }
 
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
 
 
         if (hit) {
             Debug.Log (hit.collider.gameObject.name);
         }
             
 
 
         if (hit.collider.gameObject.name ==("TL")) {
             
             Debug.Log ("indo para tl");
             TR = false;
             TL=true;
             BL=false;
             BR=false;
             INTL = false;
 
             }
         if (hit.collider.gameObject.name ==("TR")  ) {
             TR = true;
                 TL=false;
                 BL=false;
                 BR=false;
             INTR = false;
 
                 
             Debug.Log ("indo para tr");
 
         }
         if (hit.collider.gameObject.name ==("BL")  ) {
             TR = false;
             TL=false;
             BL=true;
             BR=false;
             INBL = false;
             Debug.Log ("indo para bl");
 
         }    if (hit.collider.gameObject.name ==("BR")) {
             TR = false;
             TL=false;
             BL=false;
             BR=true;
             INBR = false;
             Debug.Log ("indo para br");
 
         }    
 
 
 }
 
 
 }


what am i doing wrong?

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

137 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

Related Questions

Move gameobject when clicking on another gameobject 1 Answer

Third Person Controller - How to Click to Move? 1 Answer

Stop moving gameObject and push it back 0 Answers

Coroutine for moving lights pauses after each iteration 0 Answers

Prefab 2d interaction with 3d prefab 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