- Home /
Raycast not working in 2D game
As far as I can tell, the site ate my question the last time I typed it in. Sorry if this is a duplicate.
I'm a Unity and C# noob, and have a little experience making games. I'm trying to make a Strategy RPG similar to Fire Emblem. Characters take turns moving around on a square grid. I'm having trouble getting character movement to work. I'm using a sprite to highlight movement range. It shows up at z=1 under a character, when the player clicks on that character. When the player clicks somewhere else after selecting the character, the game uses a raycast from the cursor's (x,y) coordinates through z=1 to make sure the space to move the character to is within the movement range. However, the raycast doesn't seem to be working. I've been staring at it for a while and have no clue what's wrong. I would appreciate it if someone could help me out.
Player: using System.Collections;
public class PlayerUnit : MonoBehaviour {
bool Selected;
Vector3 CursorPosition;
int ClickTimer;
void Start () {
Selected = false;
}
// Update is called once per frame
void Update () {
if (ClickTimer >= 1) {
ClickTimer = ClickTimer - 1;
}
if (ClickTimer == 0) {
//Select unit and display movement range.
if (Input.GetMouseButtonDown (0)) {
CursorPosition = GameObject.FindWithTag ("Cursor").transform.position;
if (Selected == false) {
if ((transform.position.x == CursorPosition.x) && transform.position.y == CursorPosition.y) {
float XCenter = transform.position.x + 0.5f;
float YCenter = transform.position.y + 0.5f;
Object.Instantiate (Resources.Load ("PlayerHighlight"), (new Vector3 (XCenter, YCenter, 1)), Quaternion.identity);
Selected = true;
ClickTimer = 30;
}
}
}
}
if (ClickTimer == 0) {
//Move selected unit.
if (Input.GetMouseButtonDown (0)) {
if (Selected == true) {
if (Physics.Raycast ((new Vector3 (CursorPosition.x, CursorPosition.y, 1.5f)), Vector3.back, 1f)) {
Debug.Log ("Raycast hit something.");
transform.position = new Vector3 (CursorPosition.x, CursorPosition.y, 2);
Selected = false;
ClickTimer = 30;
}
}
}
}
}
}
Highlight sprite code (only affects color): using UnityEngine; using System.Collections;
public class MakeTransparent : MonoBehaviour {
// Use this for initialization
void Start () {
GetComponent<SpriteRenderer> ().color = new Vector4 (0f, 0f, 1f, .5f);
}
// Update is called once per frame
void Update () {
}
}
Cursor:
using UnityEngine;
using System.Collections;
public class CursorController : MonoBehaviour {
Vector2 CursorPosition;
float CursorPositionX;
int RoundedCursorPositionX;
float CursorPositionY;
int RoundedCursorPositionY;
// Update is called once per frame
void Update () {
CursorPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
CursorPositionX = CursorPosition.x;
RoundedCursorPositionX = Mathf.FloorToInt (CursorPositionX);
CursorPositionY = CursorPosition.y;
RoundedCursorPositionY = Mathf.FloorToInt (CursorPositionY);
transform.position = new Vector3 (RoundedCursorPositionX, RoundedCursorPositionY, 3);
}
}
Thank you!
vpld: $$anonymous$$y issue was that the raycast was not hitting anything when it should have been. Sorry if I was unclear.
Your answer
Follow this Question
Related Questions
having problems with my raycast shooting script 0 Answers
Calling vector3/Sprite animation/Tutorials 0 Answers
I need help with dragging objects 1 Answer
Physics2D.Raycasting questions 0 Answers
musket gun script is not working 3 Answers