Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ghoststrider · Oct 16, 2017 at 07:19 PM · rigidbodyvrvector3transform.positionweight

Cardboard VR: Drag object towards player but have it stop just before

I've been looking for questions that might ask about the problem I'm facing, but so far I haven't found anything that really gets at what I'm trying to do.

The setup is Cardboard VR, with a player using a phone and the single button. When the reticle is over an object, I want the player to be able to hit the button and pull the object towards her, but have it stop a unit or so in front of the player so it's not occupying the same world space. The speed of the move will be affected by the mass of the object's rigidbody, so the player can "feel" some weight of different objects. When it stops, I then want the object to become a child of the reticle so that weight is no longer a factor, and when you move it around it's not becoming a pain. (Although if I could get the object to follow a distance behind the reticle, that would be cool, but the reticle exists basically only on the screen and I haven't figured out how to use screenPosition for that yet.)

Here is what I have at the moment, after going through several different permutations:

A cube, to which Dragger.cs (since I originally planned on "dragging" these objects") is attached. I have a click event for Pointer Down linked to the AttachObject() function, and Pointer Up linked to DetachObject(). The result of the below code is that the cube approaches the player, but then passes right through and continues flying off the edge of the map. It's a bit of a mess as I've been trying different things. Anyone have any suggestions? I am stumped, and only now realize I probably should have asked for help a week ago.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Dragger : MonoBehaviour {
 
     
     public bool lineBreaks = true;
 
     Camera cam;
 
     private GameObject ReticleObject;
     private Transform Reticle;
     private bool IsHeld;
 
     private Vector3 dist;
     private Rigidbody rigidBody;
     private float speed;
     private Vector3 offsetPoint;
 
     // Use this for initialization
     void Start () {
         IsHeld = false;
         cam = Camera.main;
         ReticleObject = GameObject.Find("GvrReticlePointer");
         Reticle = ReticleObject.GetComponent<Transform>();
         offsetPoint = cam.transform.position + (transform.forward * 2); 
 
 
         rigidBody = GetComponent<Rigidbody>();
         // Let's make sure we have a rigidbody for the weight to be simulated
         if (!rigidBody)
         {
             rigidBody = this.gameObject.AddComponent<Rigidbody>();
         }
         speed = 5.0f / rigidBody.mass;
         dist = transform.position - offsetPoint;
     }
 
     // Update is called once per frame
     void Update () {
         if(IsHeld)
         {
             rigidBody.useGravity = false;
             Vector3 direction = offsetPoint - transform.position;
 // One thing I was trying
 //  transform.position = Vector3.MoveTowards(transform.position, offsetPoint, speed * Time.deltaTime);
             rigidBody.AddForce(speed * direction);
             if (transform.position == offsetPoint)
             {
                 rigidBody.velocity = Vector3.zero;
                 rigidBody.angularVelocity = Vector3.zero;
                 transform.SetParent(Reticle);
             }
             
         }
         else
         {
             rigidBody.useGravity = true;
             this.transform.SetParent(null);
         }
     } 
 
     public void AttachObject()
     {
         IsHeld = true;
         
     }
 
     public void DetachObject()
     {
         IsHeld = false;
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

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

134 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 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 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 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 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 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

Vector3 - transform.position 1 Answer

AddForce to a randomly selected GameObject with a rigidbody 0 Answers

Adding curve to a frisbee in virtual reality 1 Answer

Confusion in deciding the units for game objects 1 Answer

Change player movement 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