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
0
Question by jasminetay · May 22, 2015 at 02:27 PM · rigidbodycollidertransformtranslatemove object

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);
                     }
             }
 
 
         }

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

1 Reply

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

Answer by hypnoticmeteor · May 22, 2015 at 02:40 PM

  1. Create script for GameObject to be moved.

  2. 2 functions movetowards() / moveaway() and bool value.

  3. if(true) {MoveTowards()} else {MoveAway()}

  4. Raycast from main camera to object if(GO == GO) then set bool to true.

Comment
Add comment · Show 3 · 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 jasminetay · May 26, 2015 at 04:14 AM 0
Share

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);
                     }
                 }
                 
                 
             }
 
avatar image hypnoticmeteor · May 26, 2015 at 06:21 AM 0
Share
 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

avatar image jasminetay · May 27, 2015 at 09:55 AM 0
Share

I realised there were some issues with the position of my gameobject. IT WOR$$anonymous$$ED! Thank youuuuu~ :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Moving objects with attached colliders not colliding? 1 Answer

can not move my enemy with rigidbody 1 Answer

How to give jump without rigidboy by using transform? 1 Answer

Using transform in kinematics rigid body 1 Answer

Rigidbody.MovePosition relativeTo parent 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