- Home /
Move object upon hitting camera ray
I want to make the object float a few steps forward when it is in the middle of the screen/camera and float back to it's original position when the it is not. So I tried creating a ray from the middle of the screen and so far it is able to tell me the object and make it move towards the camera but i have difficulties returning the object back to its original position when the ray leaves the object...
Basically, i want it to be interactive and float towards me when im looking at it and float back to its original position when i look away from it...
This is my code:
 using UnityEngine;
 using System.Collections;
 
 public class rayobject : MonoBehaviour {
     
     public RaycastHit hit;
     public Collider collider1 = new Collider();
     private Ray ray;
     private Vector3 vec;
     private LayerMask layerMask;
     public Transform Cube1;
     GameObject origin;
     Vector3 originPos = new Vector3 (0,0,5);
     float speed;
 
     Vector3 previousPosition;
     Vector3 targetPosition;
     // Use this for initialization
     void Start () {
         speed = 0.1f;
 
     }
     
     // Update is called once per frame
     void Update () {
         
         // Find the centre of the Screen
         vec.x = (float)Screen.width / 2;
         vec.y = (float)Screen.height / 2;
         vec.z = 0;
         
         // Create the actual Ray based on the screen vector above
         ray = GetComponent<Camera>().ScreenPointToRay(vec);
 
         // This returns true if an object is hit by the ray
         if (Physics.Raycast(ray, out hit, 20.0f)) 
         {
             //stores the object hit
             collider1 = hit.collider;
             
             // Debug information - this can be deleted.
             // Draws a line to show the actual ray.
             // Outputs the name of the object hit
             Debug.DrawLine(transform.position, hit.point, Color.red);
             Debug.Log(collider1.name);
             if (collider1 == "map"){
                 collider1.transform.position = Vector3.MoveTowards(collider1.transform.position, originPos, 1);
                 //this will tell you what you are hitting
         
                     if ((Physics.Raycast (ray, out hit, 20.0f)) == false)
                     { 
                     collider1.transform.position = Vector3.MoveTowards(originPos, collider1.transform.position, 1);
                     }
             }
 
 
         }
Answer by hypnoticmeteor · May 22, 2015 at 02:40 PM
- Create script for GameObject to be moved. 
- 2 functions movetowards() / moveaway() and bool value. 
- if(true) {MoveTowards()} else {MoveAway()} 
- Raycast from main camera to object if(GO == GO) then set bool to true. 
Thank you so much! I added bool value but there are still issues with my script.... is it because of the position that i didnt manage to capture or is it due to the if else statements?
 using UnityEngine;
 using System.Collections;
 
 public class mapPlane$$anonymous$$ove : $$anonymous$$onoBehaviour {
         
         public RaycastHit hit;
         public Collider collider1 = new Collider();
         private Ray ray;
         private Vector3 vec;
         private Layer$$anonymous$$ask layer$$anonymous$$ask;
         private GameObject origin;
         private Vector3 defaultPos;
         Vector3 originPos = new Vector3 (0,0,0);
         private float speed;
         private bool hitNohit;
         
         Vector3 previousPosition;
         Vector3 targetPosition;
         // Use this for initialization
         void Start () {
             speed = 0.1f;
             defaultPos = transform.position; 
         }
         
         // Update is called once per frame
         void Update () {
             
             
 
 
 
             // This returns true if an object is hit by the ray
             if (Physics.Raycast(ray, out hit, 50.0f)) 
             {
                 if ((hit.collider.tag) == "map" && transform.position == defaultPos)
                 { 
                     hitNohit = true;
                 }//stores the object hit
                 collider1 = hit.collider;
             }
             else 
             {
                 hitNohit = false;
             }
                 // Debug information - this can be deleted.
                 // Draws a line to show the actual ray.
                 // Outputs the name of the object hit
                 Debug.DrawLine(transform.position, hit.point, Color.red);
                 Debug.Log(collider1.name);
                 if (hitNohit == true){
                     collider1.transform.position = Vector3.$$anonymous$$oveTowards(defaultPos, originPos, 5);
             }
                     //this will tell you what you are hitting
                 if (hitNohit == false && (hit.collider.tag != "map")){    
                         collider1.transform.position = Vector3.$$anonymous$$oveTowards(originPos, defaultPos, 5);
                     }
                 }
                 
                 
             }
 
 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$VT$$anonymous$$VA : $$anonymous$$onoBehaviour 
 {
 
     RaycastHit hit;
     Ray ray;
 
     Vector3 defaultPosition;
     public float distanceTo$$anonymous$$oveAway;
     public float speed;
 
     bool working, move;
 
     void Start () 
     {
         defaultPosition = transform.position;
     }
     
     void Update () 
     {
 
         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 50))
         {
             if (hit.transform.tag == "$$anonymous$$ove")
             {
                 working = true;
                 move = true;
             }
             else
             {
                 return;
             }
         
         }
 
         if (!working) 
         {
             return;
         }
         if (move)
         {
             $$anonymous$$oveTowards();
         }
         else 
         {
             ReturnPosition();
         }
     
     }
 
     void $$anonymous$$oveTowards()
     {
         if (Vector3.Distance(defaultPosition, transform.position) < distanceTo$$anonymous$$oveAway)
         {
             transform.position = Vector3.$$anonymous$$oveTowards(transform.position, Camera.main.transform.position, Time.deltaTime * speed);
         }
         else
         {
             move = false;
         }
     }
 
     void ReturnPosition()
     {
         if (Vector3.Distance(transform.position, defaultPosition) < 1)
         {
             transform.position = defaultPosition;
             working = false;
         }
         else
         {
             transform.position = Vector3.$$anonymous$$oveTowards(transform.position, defaultPosition, Time.deltaTime * speed);
         }
     }
 }
 
//Create a tag and assign that to the gameobject
I realised there were some issues with the position of my gameobject. IT WOR$$anonymous$$ED! Thank youuuuu~ :)
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                