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 /
avatar image
0
Question by DeepQuantum · Apr 24, 2020 at 06:32 AM · rotationraycast2d gametopdown

No way to use Raycast infront of player according to rotation in 2D?

Im trying to raycast in front of the player. My game is in topdown perspective and the player can move freely (so simply doing Vector2.up and Vector2.right and so forth doesn't work most of the time).

So far I've painstakingly used switch cases for the most common angles, but this is very noticable during gameplay because most angles aren't covered. The problem is that there doesn't seem to be a transform.forward equivalent for 2D, and because the the Physics2D.Raycastmethod requires a Vector2, and there's no way to convert transform.rotation.z to that required Vector.

This the code I have so far.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShootingBehaviour : MonoBehaviour
 {
     public GameObject Missile;
     public float movementSpeed = 50;
     public GameObject shootTowards;
 
     void Update()
     {   
         if (Input.GetKeyDown(KeyCode.Space))
         {
             RaycastHit2D hit = CalcHit(transform.rotation.z);
             if (hit.collider != null)
             {
                 Destroy(hit.collider.gameObject);
                 Debug.DrawRay(transform.position, hit.collider.transform.position, Color.red);
                 Debug.Log(hit.collider.gameObject.name);
             }
         }
     }
 
     public RaycastHit2D CalcHit(float rotation)
     {
         Vector2 offsetPos = new Vector2(transform.position.x + 30, transform.position.y + 30);
         switch (transform.rotation.z)
         {
             case 0:
                 return Physics2D.Raycast(offsetPos, Vector2.right, 1000f, 1 << 8);
             case 45:
                 return Physics2D.Raycast(offsetPos, new Vector2(1, 1), 1000f, 1 << 8);
             case 90:
                 return Physics2D.Raycast(offsetPos, Vector2.up, 1000f, 1 << 8);
             case 135:
                 return Physics2D.Raycast(offsetPos, new Vector2(1.5f, 1.5f), 1000f, 1 << 8);
             case 180:
                 return Physics2D.Raycast(offsetPos, Vector2.left, 1000f, 1 << 8);
             case -135:
                 return Physics2D.Raycast(offsetPos, new Vector2(-1f, -1f), 1000f, 1 << 8);
             case -90:
                 return Physics2D.Raycast(offsetPos, Vector2.down, 1000f, 1 << 8);
             case -45:
                 return Physics2D.Raycast(offsetPos, new Vector2(0.5f, 0.5f), 1000f, 1 << 8);
         }
         return Physics2D.Raycast(offsetPos, Vector2.right, 1000f, 1 << 8);
     }
 }



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

Answer by SmokiestSpace6 · Apr 24, 2020 at 06:46 AM

Try Vector2.right, there's also up and then the negatives of both, but I usually use Vector2.right and as long as the player sprite is initially facing right this should work, otherwise you can use the others mentioned.,Try Vector2.right, theres also up and then the negatives of both, but I usually use Vector2.right and as long as the player sprite is initially facing right this should work, otherwise you can use the others mentioned.

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 DeepQuantum · Apr 24, 2020 at 06:48 AM 0
Share

The problem is is that I need more than those basic few angles. I'm trying to make it that I can cast rays in all directions depending on my rotation around the z axis, not just the few that Vector2 provides. I already used Vector2.right, .up etc.

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

238 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 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

Raycast hits and then doesn't fire again? 2D Game Raycasting Problem 1 Answer

Top Down Shooter Player Controller 1 Answer

2D raycast shooting in the wrong direction 1 Answer

RaycastHit2D to hit an enemy when player rotates. (Using keyboard input only) 2 Answers

Raycasting from object rotation? 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