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
2
Question by rick-grendel · Feb 05, 2015 at 08:10 AM · raycasttransform

How to raycast always in direction of a object?

Hello,

I have a simple question but I can't answer it. How to raycast always in direction of a gameobject or transform.

Again tanks for the answers i really appreciate it!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Baste · Feb 05, 2015 at 08:40 AM

If you have a origin point you're raycasting from, you create a vector pointing in the correct direction by subtracting the origin's point from the target's point. If you're raycasting from the object that the script is attached to, it's simply:

 void RaycastTowardsThing(GameObject thing) {
     Vector3 raycastDir = thing.transform.position - transform.position;

     Physics.Raycast(transform.position, raycastDir, ...
 }
Comment
Add comment · Show 2 · 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 rick-grendel · Feb 05, 2015 at 08:42 AM 0
Share

Can you translate it to JS $$anonymous$$y c is very bad

avatar image henriquereiss403 · Sep 16, 2020 at 08:01 PM 0
Share

Hi, I got the this, and I've tried and it works! but I didn't get why we have to subtract the origin's point from the target's point, can you please help me with this?

sorry for my english, I'm not fluent.

avatar image
0

Answer by sysameca · Feb 05, 2015 at 08:35 AM

 private void Update()
 {
     RaycastHit hit;

     // Cast from the objects position upwards
     Physics.Raycast(transform.position, transform.up, out hit);
     // Cast from the objects position right
     Physics.Raycast(transform.position, transform.right, out hit);
     // Cast from the objects position forward
     Physics.Raycast(transform.position, transform.forward, out hit);
 }

Comment
Add comment · 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 rick-grendel · Feb 05, 2015 at 08:38 AM 0
Share

I search more for that i have a varible like

var Target : Transform;

and that always the raycast points to the target.

avatar image
0

Answer by Master-Krepta · Apr 09, 2016 at 05:32 PM

@baste's solution should work. but I have a similar solution in my current game that is causing an issue when i rotate my transform.

What I am going for is i want the game object to rotate in accordance with the surface normal of the cube, (basically I am trying to walk on all sides of a cube and rotate accordingly. ) My first rotation works beautifully, but the raycast always points out in a different direction and therefore never hits the gameobject again, which prevents me from being able to reorient properly.

Here is my code, if anyone has any ideas as to what I am doing wrong I would appreciate the help. Really scratching my head as to what I am missing.

 using UnityEngine;
 using System.Collections;
 
 public class OrientToSurfaceNormal : MonoBehaviour
 {
 
     public Transform worldToStickTo;
     public float gravity = -9.8f;
 
     // Raycast stuff
     public Vector3 playerNormal;
     public Vector3 surfaceNormal;
     public Vector3 fwd;
     Vector3 direction;
     
     
     
     // Use this for initialization
     void Start()
     {
         playerNormal = transform.up;
         surfaceNormal = playerNormal;
     
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         RaycastHit hitInfo;
 
         fwd = transform.TransformDirection((worldToStickTo.transform.position - transform.position));
         
         Physics.Raycast(transform.position, fwd, out hitInfo, 100); // THIS WORKS
         Debug.DrawRay(transform.position, fwd, Color.green);  // draw the debug;
 
         playerNormal = hitInfo.normal;
     }
 
     void Update()
     {
         transform.rotation = Quaternion.FromToRotation(surfaceNormal, playerNormal) * transform.rotation; // Rotation without smoothing. 
     }


Comment
Add comment · Show 2 · 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 Master-Krepta · Apr 10, 2016 at 12:51 AM 0
Share

Disregard. After more searching I found this answer to my problem http://answers.unity3d.com/answers/553805/view.html

avatar image FoolishTroll Master-Krepta · Sep 16, 2020 at 09:33 AM 0
Share

Either way keep up the excellent quality writing.

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

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

Related Questions

Transform.LookAt Seems To Cause Issues 1 Answer

Move object along ray cast using object's AddForce or velocity but not transform 1 Answer

Correct use of transform.position of UI element into game screen 1 Answer

Rotate raycast origin with gameobject 0 Answers

Raycast moved object won't go under certain height 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