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 venhip · Jul 19, 2012 at 08:02 PM · draginteractiondragginggrabphysical

Dragging objects like in Amnesia

Hello! My game needs to have some sort of physical interaction, I'd quite like to implement a sort of grab and drag mechanic, much like in Amnesia: The Dark Descent, or like in Anna (a recent indie game made in unity) Where you can simply click on an object and by moving the mouse, it will move the object being dragged around in the 3d world smoothly, does anyone know how to make such a mechanic, and if so, how please? Thanks.

Edit: I would use OnMouseEnter() but my mouse is bound to the centre of my screen and hidden so sadly on mouse enter doesn't work

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

2 Replies

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

Answer by Doireth · Jul 19, 2012 at 09:24 PM

A bit messy. (C# version)

    using UnityEngine;
     using System.Collections;
     
     public class AMnesiaPickup : MonoBehaviour {
      public Transform empty;
      public LayerMask interactLayer;
      private float dist = 0;
      
      private Transform target;
      
      // Update is called once per frame
      void Update () {
      Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     
      RaycastHit h;
      if(Physics.Raycast(transform.position, ray.direction, out h, interactLayer) && target == null) {
      if(Input.GetButtonDown("Fire1")) {
      dist = Vector3.Distance(h.point, transform.position);
      empty.position = transform.position+(ray.direction.normalized * dist);
      
      target = h.transform;
      } 
      }
      
      Rigidbody rb = null;
      
      if(target != null) rb = target.GetComponent<Rigidbody>();
     
      if(Input.GetButton("Fire1") && rb) {
      rb.isKinematic = true;
      target.transform.parent = empty;
      empty.position = transform.position + (ray.direction.normalized * dist);
      }
      
      if(Input.GetButtonUp("Fire1") && rb) {
      rb.isKinematic = false;
      target.transform.parent = null;
      target = null;
      }
      }
     }

Hope this helps.


EDIT: Javascript version and minor correction to C# version

(Javascript version)

 #pragma strict
 
 var empty : Transform;
 var interactLayer : LayerMask;
 
 private var dist : float;
 private var target : Transform;
 
 function Update () {
  var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  
  var h : RaycastHit;
  if(Physics.Raycast(transform.position, ray.direction, h, 100, interactLayer) && target == null) {
  if(Input.GetButtonDown("Fire1")) {
  dist = Vector3.Distance(h.point, transform.position);
  empty.position = transform.position + (ray.direction.normalized * dist);
  
  target = h.transform;
  }
  
  var rb : Rigidbody = null;
  
  if(target != null) rb = target.GetComponent(Rigidbody);
  
  if(Input.GetButton("Fire1") && rb) {
  rb.isKinematic = true;
  target.transform.parent = empty;
  empty.position = transform.position + (ray.direction.normalized * dist);
  }
  
  if(Input.GetButtonUp("Fire1") && rb) {
  rb.isKinematic = false;
  target.transform.parent = null;
  target = null;
  }
  }
 }
Comment
Add comment · Show 8 · 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 Doireth · Jul 19, 2012 at 09:27 PM 0
Share

Oops, I forgot. The "empty" mentioned in the script is simply an empty object assigned through the editor. Nothing special has to be done to it, just need it for parenting.

It's not a great solution I know but it may hold you over until you get another.

avatar image Ingen · Jul 20, 2012 at 02:46 PM 0
Share

Can work using this?

http://docs.unity3d.com/Documentation/ScriptReference/Transform-parent.html

 public class example : $$anonymous$$onoBehaviour {
 public Transform cameraTransform = Camera.main.transform;
 void Example() {
     cameraTransform.parent = transform;
     cameraTransform.localPosition = -Vector3.forward * 5;
     cameraTransform.LookAt(transform);

without the empty gameobject

avatar image Doireth · Jul 20, 2012 at 02:50 PM 0
Share

Can't see how it would. You don't want to parent the moving object to the camera as you will/may want the object to move based on the mouse and independent of the camera which I assume would be attached or following the player. The empty object as a parent is cheap and quick.

The empty object requires no special work or changes done to it.

avatar image Ingen · Jul 20, 2012 at 03:13 PM 0
Share

sorry I thinking for first person player

avatar image Doireth · Jul 21, 2012 at 07:57 PM 1
Share

@venhip Changed post to include JS version.

Show more comments
avatar image
1

Answer by Alayna · Jul 19, 2012 at 09:25 PM

I am using OnMouseEnter() In my game currently so that when overtop of objects they light up. My cursor is hidden and locked to the center of the screen but it still functions properly. If the script you have with OnMouseEnter() doesnt work it's possible that something else is wrong with it? (I have a reticule in the center of the screen so you can tell where the cursor is despite being hidden)

Unity already has a script built in that allows for similar function as this as well. Its under StandardAssets/Scripts/GeneralScripts/DragRigidBody. It doesnt work perfectly so you'll probably want to refine it but it could be a place to start.

Comment
Add comment · 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

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Grab and throw object immediately 1 Answer

How can i implement a scroll speed for a scroll view/rect so that my content travels slower when I drag? 1 Answer

Make EventSystem ignore a collider 0 Answers

Grab and Drag Terrain to move camera 0 Answers

Possible to drag items across tabs onto the same frame? 1 Answer


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