• 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
Question by esitoinatteso · Sep 29, 2013 at 04:59 PM · errorraycastrayorigincs0029

Raycast Ray ray origin problem

Hi there! I'm having a problem calling a Raycast to handle projectiles.

I type Physics.Raycast(Ray ray,out RayCastHit $$anonymous$$tInfo, float Distance) and then a problem occurs.

What I'm trying to do with my Raycast, is to originate it from my player position.

So I state that Ray ray = new Vector3 ( my player's x,y,z).

That line gives Error CS0029: cannot implicitly convert type " UnityEngine.Vector3" to "UnityEngine.Ray".

Now, I thought Ray ray was supposed to be a Vector3, so what's wrong?

Please, since Raycasts aren't that easy and I'm just a beginner, try to be as clear as possible in your answers... Thanks for the patience!

EDIT: My Script now is t$$anonymous$$s

 if(Input.GetKeyDown(KeyCode.Space)){
             RaycastHit $$anonymous$$tInfo;
             
             if(Physics.Raycast(transform.position, transform.forward,out $$anonymous$$tInfo, rayDistance)){
                 
                 Debug.Log (" Raycast Doing Good");
                 
             }
 }

It's working, and it's stored in the Update Function of my Player' Script.

I'd like to know how to get the same result when I Raycast from another script and I still want to origin my ray from my Player GameObject position.

I tried to store my player position in static variables to call them with Player.myPlayerPositionX and the likes, with no success at all.

IF I declare a new Public Static originRay(myPlayerPosition) where myPlayerPosition is a Vector3 I can then place the Raycast in the update of another script not related direcly with my Player GameObject and call t$$anonymous$$s originRay in Ray ray = originRay... if that's right I've got my answer.

Thanks for the feedback!

Comment

People who like this

0 Show 1
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 infinitypbr · Sep 29, 2013 at 07:35 PM 0
Share

Can you post your code?

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Hoeloe · Sep 29, 2013 at 05:26 PM

Ray is not type Vector3, it is type Ray. Types are not so fickle as that - if it says it's type "Ray" (w$$anonymous$$ch you defined it as being when you wrote Ray ray), then it is type Ray, not type Vector3. The only time somet$$anonymous$$ng like you've written is valid (i.e. T1 t = new T2()) is when T2 is a subclass of T2. Since Vector3 is a struct, it cannot be a subclass of anyt$$anonymous$$ng, so your code is invalid. Generally, it's best to be as specific as possible when it comes to types, so if you want to create a Ray, then try new Ray. T$$anonymous$$s will create a new variable of type Ray.

On another note, t$$anonymous$$nk about what a Ray is - it requires a start point, a direction and a magnitude. A Vector3 is a 3 dimensional vector, w$$anonymous$$ch by definition has a direction and magnitude... however, it doesn't have a start point, and therefore is NOT equivalent to a Ray. In fact, a Ray needs TWO Vector3 objects in order to be fully described, and the Ray class stores these in a way that is useful for Ray calculations.

I suggest, as well as reading t$$anonymous$$s, that you look for some basic Object Oriented Programming tutorials, as well as tutorials for the C# Type system.

Comment
Ashutosh8126
Jamora

People who like this

0 Show 6 · 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 esitoinatteso · Sep 29, 2013 at 07:32 PM 0
Share

Thanks for your answer Hoeloe, but I'd have appreciated more if you described it with a script example. I'm going to edit my question to make it more comprehensible.

avatar image Hoeloe · Sep 29, 2013 at 07:51 PM 0
Share

I prefer not to hand out working code, because it encourages people to "copy and paste". This is such a simple issue, though. All you need to do is call the constructor for Ray instead of Vector3. When you write new Vector3(x,y,z), you're calling a method called a constructor which makes a new Vector3 given the information provided in the arguments. Of course, creating a Ray won't need the same information as a Vector3, so instead you'll get something like this: new Ray(position, direction), where position and direction are Vector3s.

avatar image infinitypbr · Sep 29, 2013 at 08:00 PM 0
Share

@Hoeloe: I'm calling you out on this. Yeah some people may copy/paste, but most will try to understand what the code is doing. For someone who doesn't know what code works, it can be quite difficult to abstractly picture what you're talking about -- especially if their mistake is based in a current misperception of key points.

avatar image esitoinatteso · Sep 29, 2013 at 08:00 PM 0
Share

Aww! Now it's clear to me! I'll have to try that, thanks a lot for explaining yourself! So, i'll edit my question...

avatar image Hoeloe · Sep 29, 2013 at 08:06 PM 0
Share

Sorry for sounding so harsh, but I have been helping people with code for a long time, and I prefer to explain the concepts behind the code, giving people a push they need to puzzle through it themselves. It takes more work on your part, but at the end you will be guaranteed to understand it. Too many times I have given people exactly what they needed, only finding them asking exactly the same question an hour later because they didn't fully understand it.

Show more comments
avatar image

Answer by Ashutosh8126 · Sep 29, 2013 at 05:08 PM

Its not the ray i.e Vector3.. Ofcourse it is of type Ray.. is you want to set the origin of the ray... use Ray ray.origin = (Your player's position) .. :)

Comment
Hoeloe

People who like this

-1 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 esitoinatteso · Sep 29, 2013 at 07:33 PM 0
Share

Hi Ashutosh8126, I tried also that way but kept saying strange things, I'm editing my question to depict a more comprehensible picture of my problem.

avatar image

Answer by infinitypbr · Sep 29, 2013 at 08:05 PM

Not sure if t$$anonymous$$s will help, but t$$anonymous$$s code works, and will only affect objects marked with the "Player" layer.

 var attackForwardMask : LayerMask = -LayerMask.NameToLayer("Player");    // To ensure ray only $$anonymous$$ts player Layer (only the player is on that layer)
 var attackForwardHit : RaycastHit;                                        // Set up for callback
 var fwd = transform.TransformDirection (Vector3.forward);                // Ray will be shot forward
 if (Physics.Raycast (transform.position, fwd, attackForwardHit, radius, attackForwardMask))    // Looking for a $$anonymous$$t on a collider wit$$anonymous$$n the radius
 {
     Debug.DrawLine (transform.position, attackForwardHit.point, Color.yellow, 5);
     if (attackForwardHit.collider.gameObject.tag == "Player Object")    // Double check to make sure it's the player object
     {
         // DO YOUR STUFF HERE
     }
 }
Comment

People who like this

0 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 esitoinatteso · Sep 29, 2013 at 08:25 PM 0
Share

Hi sfbaystudios! Thanks for your code! It's really interesting but I'm not sure to fully understand it, but I'll study it with the help of the docs!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Changing position of a RayCast 1 Answer

A little confused about scripting Raycasts 1 Answer

camera.ScreenPointToRay always has same origin... 1 Answer

Ray.origin point moving from stationary object? 1 Answer

Help with ray cast 4 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