Quantcast
Channel: Questions in topic: "javascipt"
Viewing all articles
Browse latest Browse all 1875

Placing object where ray collides

$
0
0
I have this code so far for a script that I want to have move an object to where the mouse is looking. It works on objects, but when the ray collides with any terrain or trees from the terrain, the object move to the origin of the terrain, not where the mouse is looking. Any suggestions? #pragma strict var hit: RaycastHit; var origin: GameObject;//origin of ray var hitObject: GameObject; var hitTest: Vector3; function Start () { } function Update () { /////////////RAYCASTING var forward: Vector3= transform.TransformDirection(Vector3.forward*10); var fwd= transform.TransformDirection(Vector3.forward); Debug.DrawRay(origin.transform.position,forward*200,Color.green); if(Physics.Raycast(transform.position,fwd,hit)){ hitTest=hit.transform.position; print("Collision with: "+hit.collider.tag); hitObject.SetActive(true); hitObject.transform.position= hitTest; } else { hitObject.SetActive(false); } /////////////// RAYCASTING }

Viewing all articles
Browse latest Browse all 1875

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>