- Home /
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!
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, ...
 }
Can you translate it to JS $$anonymous$$y c is very bad
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.
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);
 }
I search more for that i have a varible like
var Target : Transform;
and that always the raycast points to the target.
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. 
     }
Disregard. After more searching I found this answer to my problem http://answers.unity3d.com/answers/553805/view.html
Either way keep up the excellent quality writing.
Your answer
 
 
             Follow this Question
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
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                