• 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 Yizzard_Palmero · Jul 07, 2014 at 05:50 PM · c#raycastraycastingorigin

How do I change the origin point of a raycast?

 using UnityEngine;
 using System.Collections;
 
 public class Climbing : MonoBehaviour
 {
     void Update()
     {
         var rayRange = 10;
         var rayDirection = transform.TransformDirection(Vector3.forward);
         if(Physics.Raycast(transform.position + transform.up * 0.75, rayDirection, RaycastHit, rayRange))
         {
             Debug.Log("hello world");
         }
     }
 }

Assets/Scripts/Climbing.cs(10,59): error CS0019: Operator *' cannot be applied to operands of type UnityEngine.Vector3' and `double'

please help i have looked up so many tutorials but i just cant move the origin of the raycast.

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · Jul 08, 2014 at 06:12 PM

In addition to the problem @thomasindustry points out (w$$anonymous$$ch is the source of the error you are asking about), you are using a type where a variable should be when you use RaycastHit for the t$$anonymous$$rd parameter. The parameter needs to be a variable of that type, plus you need the 'out' keyword since the value is returned in the variable. Also when coding in C#, generally I make parameter explicit rather than use 'var'. So here is a rewrite of your code that will compile:

 using UnityEngine;
 using System.Collections;
 
 public class Climbing : MonoBehaviour
 {
     void Update()
     {
         float rayRange = 10.0f;
         Vector3 rayDirection = transform.TransformDirection(Vector3.forward);
         RaycastHit $$anonymous$$t; 
         if(Physics.Raycast(transform.position + transform.up * 0.75f, rayDirection, out $$anonymous$$t, rayRange))
         {
             Debug.Log("hello world");
         }
     }
 }
Comment

People who like this

0 Show 5 · 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 Yizzard_Palmero · Jul 09, 2014 at 10:32 PM 0
Share

well... now its compiling with no errors but even when i walk in front of things the "hello world" isn't triggering... I even tried removing the * .75f, part just in case it was too high but still it wouldn't trigger. There's probably some really easy fix to this but I just started programming.

avatar image robertbu · Jul 10, 2014 at 12:52 AM 0
Share

Start by creating a Debug.DrawRay() for your raycast to check things out:

 Debug.DrawRay(transform.position + transform.up * 0.75f, rayDirection * rayRange);
avatar image Yizzard_Palmero · Jul 10, 2014 at 01:00 AM 0
Share

It just gave me a bunch of compiler errors when I put that in. I added it and I got errors I replaced it with the old code and I got errors and I replaced the if(.....) part with it and got errors. Is that not where I was supposed to put it or what?

avatar image robertbu · Jul 10, 2014 at 01:04 AM 0
Share

I would add it just above the raycast (between lines 10 and 11 in the code I provided). Note that I forgot the 'f' on the end of '0.75f' in my original Debug.DrawRay() comment.

avatar image Yizzard_Palmero · Jul 10, 2014 at 01:07 AM 0
Share

O I'm retarded. I forgot to capitalize the u in Update. It's working now. Thanks so much for your help!

avatar image

Answer by thomasindustry · Jul 07, 2014 at 05:53 PM

Change your 0.75 to 0.75f, otherwise your code looks right for moving the ray origin 0.75 units above the object.

T$$anonymous$$s will make it a float instead of a double w$$anonymous$$ch unity prefers (quicker calculations but less precise).

Comment
robertbu

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 Yizzard_Palmero · Jul 08, 2014 at 06:01 PM 0
Share

that just brought up a new error that says cannot convert object expression to type float

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

Raycast function doesn't work. 1 Answer

[C# / Unity] Raycast Ignoring Self / Offsetting Instantiations? 0 Answers

[C#] UNET Client raycast not the same as Server raycast 2 Answers

Multiple Cars not working 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