• 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 /
avatar image
0
Question by Olbaper · Nov 27, 2016 at 08:31 AM · 2dgameobject2d gameclick to movezone

Zone interactable and no-interactable with an object Unity 2d

Hello everyone! (I'm spanish so excuse my bad english pls) I'm working on a 2d game, my problem is that I have a player, which moves with the mouse(where you click, the object goes right there) but I want to create a zone (a section of te screen) where if you click because you want the object to move there the object doesn't move. In resume, if you click out of the zone, the object does nothing. If someone could help me I'll be so thankful.

This is my code for the moving script:

 using UnityEngine;
 
 public class GotoMouse:MonoBehaviour{
 
 public float speed = 1.5f;
 private Vector3 target;
 public bool canMove = true;
 public GotoMouse player;
 
 void Start () {
     target = transform.position;
 
     player = FindObjectOfType<GotoMouse> ();
     player.canMove = true;
 
 }
 
 void Update () {
 
 
     if(!canMove)
     {
         return;
     }
 
     if (Input.GetMouseButtonDown (0)) {
 
         target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         target.z = transform.position.z;
         player.canMove = true;
 
     } 
 
     transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
 
     player.canMove = true;
 
      }
 }    




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 geeneth · Nov 17, 2020 at 06:45 AM 0
Share

Use Raycast2D downward and make it check for notmovable layer gameObject and if the raycast detects a gameObject with the layer notmovable make can move bool to false.

 Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
 RaycastHit2D ray = Physics2D.Raycast(mousePos, Vector2.down, whaIsNot$$anonymous$$ovable)
 
 if(ray.collider != null){
         can$$anonymous$$ove = false;
 }else{
          can$$anonymous$$ove = true;
 }

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Setting GameObjects inactive/invisible by default in GUI? 2 Answers

How to activate an object after another is deactivated 1 Answer

Flip the player with arm rotation 2 Answers

Quality decrease after build 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges