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 Safforn · May 27, 2013 at 02:03 PM · c#collisionraycasttransformtranslate

Help with casting a raycast C#

Hello,

I have a game where a block moves and the player has to navigate it through gates. The script I'm using uses transforn.translate to move the cube forward and sideways. But now it has no colliders because that method ignores them. The solution is to use a raycast. I have no idea how to do this though, I can't get it to work. Can someone give the proper code? Thanks you.

my code:

 using System.Collections;
 
 public class testMotor
 : MonoBehaviour {
  
 public float MoveSpeed = 40;
 public float SideSpeed = 10;
 
  
 void Update () 
 {
 
     float MoveForward = MoveSpeed * Time.deltaTime;
     float MoveSide = Input.GetAxis("Horizontal") * SideSpeed * Time.deltaTime; 
  
     
         
     transform.Translate(Vector3.forward * MoveForward);
     transform.Translate(Vector3.right * MoveSide);
  
     
         
         
 }
 }
Comment
Add comment · Show 3
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 robertbu · May 27, 2013 at 02:09 PM 0
Share

What are you trying to solve with a Raycast? Are you trying to simulate collisions?

avatar image Safforn · May 27, 2013 at 02:16 PM 0
Share

Yes, the gates need to detect when the cube gets through and also if it's the right one. And when the cube has reached the end of the track it needs to go to the next level. And to prevent it from going off the course and out of camera sight.

avatar image drawcode · May 27, 2013 at 02:22 PM 0
Share

Sounds like you could also solve this with checking rect/boundaries and or triggers in the gates. If you are looking for collisions and ai try a navmesh agent possibly. Hard to tell the best solution from this. Also you probably want colliders on gates and boxes if there aren't too many of them. If there are then you'll have to shoot a ray out of each side however you will again need colliders for this.

1 Reply

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

Answer by drawcode · May 27, 2013 at 02:24 PM

C#

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
     void Update() {
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         if (Physics.Raycast(transform.position, fwd, 10))
             print("There is something in front of the object!");
         
     }
 }

Distance based

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
     void Update() {
         RaycastHit hitForward;
         RaycastHit hitBack;
         RaycastHit hitLeft;
         RaycastHit hitRight;

         // forward
         if (Physics.Raycast(transform.position, Vector3.forward, out hitForward)) {
             float distanceForward = hitForward.distance;
         }

         // back
         if (Physics.Raycast(transform.position, -Vector3.forward, out hitBack)) {
             float distanceReverse = hitBack.distance;
         }

         // left
         if (Physics.Raycast(transform.position, Vector3.left, out hitLeft)) {
             float distanceLeft = hitLeft.distance;
         }

         // right
         if (Physics.Raycast(transform.position, Vector3.right, out hitRight)) {
             float distanceRight = hitRight.distance;
         }
         
     }
 }
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

14 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

Related Questions

Dashing through enemies, but not walls. 1 Answer

Transform.Position Collision Issue 0 Answers

AttackDammage errors 1 Answer

Raycast from camera not hitting Terrain 1 Answer

Move until collision sticks 0 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