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 mr_flopps · Oct 12, 2017 at 06:29 PM · unity 52dc# tutorial

raycast 2D script issue

Hi there,

First time posting here. I am a 3D animator, and about a month ago I decided to start learning Unity and C#.

I am loving it so far. Extremely challenging and I am obviously still a complete noob. I hope that I am pasting this in the correct place.

I am busy following this tutorial and loving it - https://www.youtube.com/watch?v=clKUnv7qQuM&list=PL2cNFQAw_ndx1ID-DuvFoeGR_nE4jOFTq∈dex=13

All is going great but I am trying to do it in 2D. All has worked perfectly well up until now.

Seeing RayCast, RayCastHit and Physics are called in the bullet script. They do not collide with my 2D objects that are in the 'Shootable' layer. (Because they are using 2D colliders and 2D rigidbody's)

Seeing I don't really know what I am doing, I thought I could change the Raycast, RayCastHit and Physics to their 2D equivalents (IE Raycast2D, etc). Turns out I was well wrong. I get the error "cannot convert from unityengine.ray2D to unityengine.vector2"

Here is the script in question, apologies for the long post. Thank you very much for your time. I want to get this to work with my 2D colliders and I assume '2D physics' but no clue what to do.

public class shootBullet : MonoBehaviour {

 public float range = 10f;
 public float damage = 5f;

 Ray shootRay;
 RaycastHit shootHit;
 int shootableMask;
 LineRenderer gunLine;

 // Use this for initialization
 void Awake () {
     shootableMask = LayerMask.GetMask("Shootable");
     gunLine = GetComponent<LineRenderer>();

     shootRay.origin = transform.position;
     shootRay.direction = transform.forward;
     gunLine.SetPosition(0, transform.position);

     if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
     {
         //hit an Enemy goes here
         gunLine.SetPosition(1, shootHit.point);
     }
     else gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
 }
 
 // Update is called once per frame
 void Update () {
     
 }

}

Much thanks,

Damian

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 mr_flopps · Oct 12, 2017 at 08:51 PM

If I change the raycasting to 2D and the 17th line to the following, the errors go away but still doesn't work.

     if (Physics2D.Raycast(shootRay.origin, shootRay.direction, range, shootableMask)) 

Now if I shoot in the direction of an object that is on the Shootable layer, the gun shoots but I can see the line renderer goes off at a weird angle behind the character.

Completely stumped, any help would be greatly appreciated!

Thank you!

Damian

Comment
Add comment · 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

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

199 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

Related Questions

Unity's 2D Gamekit Input problem 1 Answer

How do I make animation transitions a one-way street? 1 Answer

How do I change the speed at which the slider moves? 5 Answers

Unity 2D Object Smooth Follow Problem C# 2 Answers

Unity 2D Car Side Scroller question. 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