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 Brutalitywarlord · May 04, 2016 at 02:06 PM · c#2d gamespritesvector2selecting

Selecting a game object in Unity 2d

I am building a 2D turn based war game, the first obstacle i must overcome in making the script for the player is giving it the ability to select a sprite and then to either move that sprite to another location that is not occupied, or attacking another sprite, but for the time being I'm focusing on selecting a sprite, and then moving it, below is the code I've written so far, it does not function in selecting the sprite, selecting the sprite is my biggest issue at the moment

using UnityEngine; using System.Collections;

public class PlayerBehavior : MonoBehaviour {

 public bool PlayersTurn;
 public int ActionsPerTurn;
 public BoardManager boardManager;
 
 private Vector3 SelectedPosition;
 private bool enemysTurn;
 void Start () {
     //boardManager = GetComponent<BoardManager> ();

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

 }
 GameObject ClickSelect()
 {
     //Converting Mouse Pos to 2D (vector2) World Pos
     SelectedPosition = new Vector2(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
     RaycastHit2D hit=Physics2D.Raycast(SelectedPosition, Vector2.zero, 0f);
     
     if (hit)
     {
         Debug.Log(hit.transform.name);
         return hit.transform.gameObject;
     }
     else return null;
 }
 

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · May 04, 2016 at 02:31 PM

@Brutalitywarlord

Here this has been tested and works ClickSelect()

 using UnityEngine;
 using System.Collections;
 
 public class PlayerBehavior : MonoBehaviour
 {
     public bool PlayersTurn;
     public int ActionsPerTurn;
     public BoardManager boardManager;
 
     private Vector3 SelectedPosition;
     private bool enemysTurn;
 
     void Start ()
     {
         //boardManager = GetComponent<BoardManager> ();
     }
 
     // Update is called once per frame
     void Update ()
     {
     }
 
     GameObject ClickSelect()
     {
         //Converting Mouse Pos to 2D (vector2) World Pos
 
         RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
 
         if (hit)
         {
             Debug.Log(hit.transform.name);
             return hit.transform.gameObject;
         }
         else return null;
     }
 }
Comment
Add comment · Show 2 · Share
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 Brutalitywarlord · May 05, 2016 at 02:23 PM 0
Share

your code has failed, Im thinking it's a different error all together, do i need to first use the grid-positions.add thing in order to add new vector3's to a grid to make this work, this function existed in the rogue like, but it doesn't work for me in my code so i removed it as i didn't need it to generate my game board, do i need to make grid positions before i can use this?

avatar image Brutalitywarlord Brutalitywarlord · May 05, 2016 at 03:08 PM 0
Share

after some small work i have manged to make the code function, thank your for your assistance my friend

avatar image
0

Answer by Semni · May 17, 2017 at 01:59 PM

You can also use Physics2D.OverlapPoint(Camera.main.ScreenToWorldPoint(Input.mousePosition); for getting a collider that overlaps a point on the screen.

Comment
Add comment · Share
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
0

Answer by umair042 · Feb 22, 2021 at 01:48 PM

simply copy and paste your problem will be fix:

    Vector2 mousepos; 
 Vector2 mousepos2;
 BoxCollider2D col;
 bool selected = false;
 bool move = false;
 RaycastHit2D hit;
 int clicks= 0;
 // Use this for initialization
 void Start () {
     mousepos2 = Camera.main.ScreenToWorldPoint (Input.mousePosition);
 }
 
 // Update is called once per frame
 void Update () {
     mousepos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
     if(Input.GetMouseButtonDown(0) && clicks == 0){
         Selected ();
     }
     Slectednotselected ();

     if(move){
         
         Move ();
     }
 }

  void Selected(){
     hit = Physics2D.Raycast (mousepos, Vector2.zero);
     if (hit.collider.GetComponent<Rigidbody2D>() != null) {
         selected = true;
         clicks = 1;
         print (hit.collider.name);
         //hit.transform.position = Vector2.MoveTowards (hit.transform.position, mousepos, 5 * Time.deltaTime);
     }else{
         selected = false;
         print ("by");
     }
         }
     
 void Move(){
     hit.transform.position = Vector2.MoveTowards (hit.transform.position, mousepos2, 5 * Time.deltaTime);
 }
 void Slectednotselected(){
 
     if (selected && Input.GetMouseButtonDown(0)) {
         mousepos2 = Camera.main.ScreenToWorldPoint (Input.mousePosition);
         print ("move");
         move = true;
     } else if (selected && Input.GetMouseButton (1)) {
         move = false;
         clicks = 0;
         selected = false;
     }
 }

}

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

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

157 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

Related Questions

I need help with AI,Force not working 0 Answers

Sprites being skipped over in sprite change script 1 Answer

Randomize text position for 2D Quiz C# 0 Answers

Sprite Shape Not Working In Unity 2018 0 Answers

Detecting a button click 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