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 /
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 $$anonymous$$ · 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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to activate an object after another is deactivated 1 Answer

Distribute terrain in zones 3 Answers

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

Double sided 2d sprite 1 Answer

(2d) How can I destroy a gameObject when I pass over it? 1 Answer


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