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;
}
}
Answer by TBruce · May 04, 2016 at 02:31 PM
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;
}
}
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?
after some small work i have manged to make the code function, thank your for your assistance my friend
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.
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;
}
}
}
Your answer
Follow this Question
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