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 TeozTroy · Sep 21, 2015 at 09:34 AM · 2dinstantiateraycastraycasthit2d

Trying to get a 2D RayCast to instantiate a GameObject

Hi everyone, as the title suggests, I've been trying to create a 2d RayCast. I'm fairly new to C# and Unity as a whole, so I'm probably missing something fairly simple to see!

I want this raycast to detect shootable layers that I define. Firstly, the Debug.DrawRay doesn't show anything, secondly, the shoot method doesn't perform, nor does it show the Debug.Log message I placed within.

 using UnityEngine;
 using System.Collections;
 
 public class ProjectileSystem : MonoBehaviour {
     //SUMMARY
     //These array lists can be added to as the game progresses. 
     //This array list allows for a neat and tidy code structure without dumping too many
     //unneccesary lines of code for float declarations.
     //
     //
     //Declaring an open array list for various ranged projectiles.
     public int[] projectileDamage = new int[] { 15, 20 };
     //Declaring an open array list for float timers between allowed shot projectiles
     public float[] timeBetweenShots = new float[] { 0.5f, 0.6f, 2f };
     //Declaring an open array list for range of various projectiles
     public float[] rangeForShots = new float[] { 45f, 60f };
 
     public GameObject arrow;
     public float speed;
 
 
     //Private declaration list
     float timer;
     Ray2D projectileRay;
     RaycastHit2D projectileHit;
     int shootableMask;
 
 
     // Use this for initialization
     void Awake () {
         shootableMask = LayerMask.GetMask("Shootable");
     }
     
     // Update is called once per frame
     void Update () {
         timer += Time.deltaTime;
         //We'll start with the basic arrow array first.
         if (Input.GetButton("Fire1") && timer >= timeBetweenShots[0])
         {
             Shoot();
         }
     }
 
     void Shoot()
     {
         timer = 0f;
 
         //TODO
         //Add in instantiation of arrow projectile sprite
          
 
         projectileRay.origin = transform.position;
         projectileRay.direction = transform.forward;
 
         if(Physics2D.Raycast (projectileRay.origin, projectileRay.direction, rangeForShots[1], shootableMask))
         {
             Instantiate(arrow, transform.position, Quaternion.LookRotation(projectileRay.direction));
             Debug.DrawRay(transform.position, projectileRay.direction, Color.green);
         }
     }
 }
 

How would I go about getting the 2D raycasts to show, and also to instantiate an object based off of the start position and following through with the direction?

Many thanks in advance!

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Error with 2D Raycast 0 Answers

2D Raycast over multiple objects not working? 1 Answer

2D - finding object at mouse position 2 Answers

Is there a better way to find where to start my raycast? 2 Answers

Raycast2D will point right, but won't point left when player turns. 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