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 xAxiom · Feb 08, 2014 at 07:39 AM · 2draycastdirectionaxisvector2

How can I raycast the direction my 2D character is facing?

Hello,

I am making a top down 2D game in unity, and I'm using a ray cast in the direction my character is facing in order to detect melee hits on enemies.

Right now I have this:

 void meleeAttack()
     {
         if(Input.GetMouseButtonDown (0)) {
             float x = Input.GetAxis("Horizontal");
             float y = Input.GetAxis ("Vertical");
 
             foundHit = Physics2D.Raycast(transform.position, new Vector2(x,y), dist, 1<<8);
             Debug.DrawRay(transform.position, new Vector2(x,y), Color.green);
         }
     }

I figured I could use the horizontal and vertical axis from the input in order to determine the direction my character is facing in. This worked perfectly except I need to be moving for this to work.

Is there any way that I can store the previous values from the x and y axis so that it will cast in the last direction when you stop moving? Or is there some kind of workaround to this?

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

1 Reply

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

Answer by robertbu · Feb 08, 2014 at 07:44 AM

Typically with a 2D sprite either the right side or the up side is the forward. So you can use either transform.right or transform.up of the character in your Raycast(). Assuming this script is on the character and 'front' is to the right:

 foundHit = Physics2D.Raycast(transform.position, transform.right, dist, 1<<8);
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 xAxiom · Feb 08, 2014 at 07:56 AM 0
Share

Yeah that will only cast it to the right of the character. Since it's topdown and I can move in any direction, my character can be "facing" left, right, up, or down.

The current code I have allows you to cast in the current direction your moving, but you can only cast in that direction while you're moving.

So the x and y variables are getting the direction and the vector will cast in that direction. So (0,1),(1,0),(1,1),etc. But the thing with using Input.GetAxis is that when you stop moving everything returns to (0,0) and it won't cast in any direction.

I'm asking if there's any way I can either store you previous direction when you stop moving, or have a workaround to the coordinate direction system.

avatar image robertbu · Feb 08, 2014 at 04:51 PM 0
Share

If the only information about direction comes from the Input.GetAxis(), you can handle things this way:

 Vector3 previousGood = Vector3.zero;

 void meleeAttack()
 {
    if(Input.Get$$anonymous$$ouseButtonDown (0)) {
      float x = Input.GetAxis("Horizontal");
      float y = Input.GetAxis ("Vertical");

      Vector3 dir = new Vector2(x,y);
      if (dir == Vector3.zero)
      {
          dir = previousGood;
      }
      else
      {
          previousGood = dir;
      }
 
      foundHit = Physics2D.Raycast(transform.position, dir, dist, 1<<8);
      Debug.DrawRay(transform.position, , Color.green);
    }
 }

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

18 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

Related Questions

Hop from transform to transform in 2D game? 0 Answers

Ray Casting in 2D 1 Answer

Raycast direction problem 3 Answers

How do I rotate arround an object by swiping left or right? 0 Answers

How to rotate object to cursor? 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