Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Hamesh81 · Mar 07, 2012 at 06:38 AM · raycastpositionmultipleoffset

How can I offset a raycast along the transform's local x/z axis?

What I am trying to achieve:

alt text

If I need to shoot say 3 raycasts (C#): 1 from transform.position (blue line), 1 to the left of this by an offset of lets say 2 units (left yellow line), and 1 to the right by the same offset (right yellow line), how can I do this? I imagine it could be something like:

transform.position + publicVariable (and)

transform.position - publicVariable

Here is my script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAI3 : MonoBehaviour {
     public Transform target;
     public int raycastLength = 1;
     public float leftRaycast = 2f;
   public float rightRaycast = -2f;
     
     public bool raycastHitting = false;
     public bool raycastHittingL = false;
     public float turnSpeed = 5;
            
     private Transform myTransform;
     
     void Awake() {
         myTransform = transform;
     }
 
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         target = go.transform;
         leftRaycast.x -= 2;
         rightRaycast.x += 2;
     }
     
     // Update is called once per frame
     void FixedUpdate () {

         //Start Raycast forward
         if(Physics.Raycast(myTransform.position, myTransform.forward, raycastLength)){
             Debug.DrawLine(myTransform.position, myTransform.forward, Color.blue);
             myTransform.Rotate(Vector3.up, -90 * turnSpeed * Time.smoothDeltaTime);
             raycastHitting = true;
             
         }
         if(!Physics.Raycast(myTransform.position, myTransform.forward, raycastLength)){
             raycastHitting = false;    
         }
         //End Raycast
         
         

         //Start Raycast Left
         if(Physics.Raycast(myTransform.position, myTransform.right * leftRaycast, raycastLength)){
             Debug.DrawLine(myTransform.position, myTransform.right * leftRaycast, Color.yellow);
             myTransform.Rotate(Vector3.up, 90 * turnSpeed * Time.smoothDeltaTime);
             raycastHittingL = true;
             
         }
         if(!Physics.Raycast(myTransform.position, myTransform.right * leftRaycast, raycastLength)){
             raycastHittingL = false;    
         }
         //End Raycast
     }
 }



What is the best way to do this?

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
1

Answer by Berenger · Mar 07, 2012 at 07:03 AM

  • The class Transform don't have an x property, but a position, which is a Vector3 and have an x property.
  • You them any. If I understand correctly, you want to cast three parallele rays from position with an offset on x. In that case, declare two public float offset1, offset2, then your three rays' origines will be

transform.position + transform.right * offset1; // for instance, offset1 = -2
transform.position + transform.right;
transform.position + transform.right * offset2; // for instance, offset2 = 2

  • If you need the case where the ray don't hit anything, do not cast it again with !, use else instead.
  • By using the same bool for all the rays (raycastHitting), it's will always take the last value, hiding the other. Just in case you need that bool, which you don't in the ewample above.
Comment
Add comment · Show 4 · 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 Hamesh81 · Mar 07, 2012 at 09:49 AM 0
Share

Thanks for your help. I threw in a diagram up top to better show what I'm trying to do. The blue line is the first raycast (mytransform.position) in the script and the left yellow line is the left raycast (myTransform.position, myTransform.right * leftRaycast). I've changed the second raycast to as you suggested but it changes the angle of the raycast it doesn't actually offset it. Or have I done it wrong?

avatar image Berenger · Mar 07, 2012 at 03:36 PM 0
Share

$$anonymous$$eep in $$anonymous$$d that a ray is composed of two things, an origin and a direction. Saying "the first raycast (mytransform.position)" makes no sens if you don't add the direction (I suppose you imply it's forward there).

So you want to cast a ray forward, to the right and to the left, with an offset on the origine : the the three lines of codes in my answer. the direction don't need to be multiplied, unless you want to invert it. In your case, and respectively with the origines I gave you, directions are transform.left, transform.forward and transform.right.

avatar image Hamesh81 · Mar 09, 2012 at 03:22 PM 0
Share

Thanks for trying to help Berenger, but I couldn't get your solution to work. I ended up using a Vector3 variable to deter$$anonymous$$e the direction which allowed me to set a ".x" offset for the other raycasts. All is gd now :D

avatar image marlon_1_23 · Jul 21, 2017 at 09:42 PM 0
Share

Berenger,

Thanks ! It worked for me .

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Raycasts 3 Answers

Lerp isn't completing itself 3 Answers

Offset Position 1 Answer

raycast object instance offset? 1 Answer

Getting position where raycast goes offscreen 2 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