Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 isetrh · May 28, 2015 at 06:31 AM · raycastnoobrpg

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!

Comment
Add comment · Show 2
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 virgiliu · May 28, 2015 at 09:38 AM 1
Share

Does your raycast hit anything?

avatar image isetrh · May 28, 2015 at 11:12 PM 0
Share

vpld: $$anonymous$$y issue was that the raycast was not hitting anything when it should have been. Sorry if I was unclear.

1 Reply

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

Answer by abi-kr01 · May 28, 2015 at 10:01 AM

your ray-cast will not work as physics is different for these 2 one is in 2dphysics and another is in 3d Try This

Comment
Add comment · Show 1 · 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 isetrh · May 28, 2015 at 11:12 PM 0
Share

This works. Thank you!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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