Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by alone1992 · Dec 08, 2012 at 09:41 AM · triggerray cast

How to use two rays (ray casting)in one object?

Hi all I write this code for my game object but it doesn't work for that but when I delete the second part of that for example left hand side It work properly!! My main question is this: how can I use two rays in one object? And how can control my game object when it move between two another objects? When I use triggers because of the high speed of objects it don't work properly most of times. My Code:

 var hSliderValue:int = 2;
 var MovingSpeed : int = 2;

 function Update ()
 {
     var hit : RaycastHit;
     var right = -transform.TransformDirection(Vector3.forward);
     var left = transform.TransformDirection(Vector3.forward);
     transform.Rotate(0, 0, Input.GetAxis("Mouse X") * hSliderValue);
     transform.Translate(0, 0, Input.GetAxis("Vertical") * MovingSpeed * Time.deltaTime);   
      
     //Compute the Right hand side
     Debug.DrawRay(transform.position, right * 0.025, Color.green);
     if(Physics.Raycast(transform.position, right, hit, 0.025))
     {
         if(hit.collider.gameObject.name == "right" && Input.GetAxis("Vertical") < 0 )
         {
             MovingSpeed = 0;
         }
         else
         {
             MovingSpeed = 2;
         }
        
     }
     
     //Compute the left hand side
     Debug.DrawRay(transform.position, left * 0.25, Color.red);
     if(Physics.Raycast(transform.position, left, hit, 0.25))
     {
         if(hit.collider.gameObject.name == "left" && Input.GetAxis("Vertical") > 0 )
         {
             MovingSpeed = 0;
         }
         else
         {
             MovingSpeed = 2;
         }
     }
 }

Excuse me dear friend. My game is 3d and in this scene we have 3 objects two of them in two sides like border and one of them in the middle. the middle object is moving some times fast and some times slow. At first time I use colliders but most of times in high speed they don't work and my middle object pass border objects. Because of that problem I change that to ray casting and now when I use it to just one side it work but when I use it for two sides it don't work for any side. you can imagine I have Foosball game and the middle object is player and two others is walls of table and player shouldn't pass the walls. I exactly don't know how to use multiple Raycasts at same time and think my code have problem.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by alone1992 · Jan 30, 2013 at 11:29 AM

The problem is in the structure of my code I should put the left hand side code in the "else{}" of the right hand side or divide them to two part and put each one in separate scripts and next add them to my object

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
avatar image
0

Answer by Bunny83 · Dec 08, 2012 at 12:24 PM

There's no problem using multiple Raycasts at the same time. However I'm not entirely sure what this code should do. I guess you want to stop the objects movement when it hits your "borders".

What do you mean with "don't work properly most of times". An explanation of what it should do would be great. Also we don't have your game scene. We don't know where the colliders named "left" and "right" are, what kind of game it is (2d / 3d).

The z-axis is usually forward in Unity but your raycasts are done to right and left (on the x-axis). Can you explain what's the purpose of the raycasts? Also a screenshot or scribble of your scene would be great. Feel free to edit your question

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

10 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Change skybox at runtime? 2 Answers

Getting audio.PlayOneShot to work 2 Answers

Safe area from enemies 1 Answer

Detect collision/trigger between two body without rigidbody? 3 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