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 artsdcs · Dec 16, 2011 at 09:01 PM · rigidbodydragmovingdraggingthrowing

moving objects in like in amnesia

i always found it realy enjoyable in amnesia you could pick things up and move them around ans stuff. i would like that to. and since i'm very new an still experimenting and stuff i would like to know if there ia any script where i could just simply pick up an object and throw it no rotating. i already tried the dragrigidbody script you get with unity but you need to touch the rigid body from very close and i dont want tha. i want to be able to pick it up from a distance. i searched long on the internet and haven't found anything i could use so please could somebody help me with it i'm scripting everything in javascript. i already have a mouse lock and a crosshair the controls i would like are:gold mouse0 to lift it and press mouse1 to throw it. i hope this is enough information and if someone know if there is already anscript like i'm describing please point me to it.

sry for any spelling mistakes i'm dutch.

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

8 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by BarkShark · Dec 17, 2011 at 12:04 PM

Alright I guess you're new to scripting, You have a couple basic mistake such as: Your action aren't in the function brackets. It should be:

 function Update()
 {
 //action
 }

Your if statement isn't correct either, you should have

 if(//condition is true)
 {
 // do an action
 }

Basic code to do what your want:

 var hitObject : GameObject;
 
 function Update()
 {
 
 if(Input.GetMouseButtonDown(0))  
 {
 
 var hit : RaycastHit;
 
 // Cast a ray
 
 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
 {
 hitObject = hit.collider.gameObject;
 hitObject.transform.parent = gameObject.transform;
 }
 }
 }
 
 if(Input.GetButtonDown("Release")) // This will release the object 
 {
 hitObject.transform.parent = null;
 hitObject = null;
 }





[1]: http://unity3d.com/support/documentation/ScriptReference/Input.html

Comment
Add comment · Show 9 · 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 artsdcs · Dec 17, 2011 at 12:37 PM 0
Share

nope i get a few errors:

Assets/object moving.js(2,8): UCE0001: ';' expected. Insert a semicolon at the end.

Assets/object moving.js(2,17): UCE0001: ';' expected. Insert a semicolon at the end.

Assets/object moving.js(4,1): BCE0043: Unexpected token: if.

Assets/object moving.js(4,33): UCE0001: ';' expected. Insert a semicolon at the end.

Assets/object moving.js(7,1): BCE0043: Unexpected token: var.

Assets/object moving.js(7,4): UCE0001: ';' expected. Insert a semicolon at the end.

Assets/object moving.js(7,5): BCE0044: expecting EOF, found 'hit'.

that are all of them. i attached it to my main camera and called the script object moving. oh and yes i am realy new to scripting=) but i'm trying to learn.

avatar image BarkShark · Dec 17, 2011 at 01:05 PM 0
Share

Sorry I made the code in a rush. There was a typo, I wrote 'fuction ins$$anonymous$$d of function'. There was also a bracklet missing. I've edited the code, normally it should work now.

avatar image artsdcs · Dec 17, 2011 at 01:21 PM 0
Share

i must say what happens now is pretty funny becouse i can now lift up the intire floor and stuff. so can it be edited so that it only works on tagged objects or riggidbody's also i can't yet release something it comes up with this error:

UnityException: Input Button Release is not setup. To change the input settings use: Edit -> Project Settings -> Input object moving.$$anonymous$$ain () (at Assets/object moving.js:23)

and i figured out there was a tiny mistake left so i changed it a bit:

var hitObject : GameObject;

function Update() {

if(Input.Get$$anonymous$$ouseButtonDown(0))
{

var hit : RaycastHit;

// Cast a ray

if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit)) { hitObject = hit.collider.gameObject; hitObject.transform.parent = gameObject.transform; } } }

if(Input.GetButtonDown("Release")) // This will release the object { hitObject.transform.parent = null; hitObject = null; }

avatar image BarkShark · Dec 17, 2011 at 01:29 PM 0
Share

what was the tiny mistake? I can't see it.

avatar image artsdcs · Dec 17, 2011 at 01:34 PM 0
Share

i dont remember but there was somewhere a random { or } probably my fault i dont know. but it still doesn't work properly sry.

Show more comments
avatar image
0

Answer by artsdcs · Dec 16, 2011 at 11:37 PM

so i'm thinking something like this(it doesn't work yet) i atleast know i still need to add the mouse button and some other stuff but as far is i know something about scripting i guess this is the base of the it so what could i do to let it work??

function Update () { }

if var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

if (Physics.Raycast (ray, 100)) { print ("Hit something");

// make it a child of the current object cameraTransform.parent = transform;

else // Detaches the transform from its parent. transform.parent = null;} }

probably full of mistakes but i'm new and like many people i try to learn it bit by bit.

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
avatar image
0

Answer by artsdcs · Dec 18, 2011 at 01:17 PM

i realy had no idea where to start but i came to this:

 var hitObject : GameObject;
 
 function Update()
 {
 
 if
 // By tag
     var player = GameObject.FindWithTag("Pick");
     player.GetComponent(OtherScript).DoSomething();
 }
 
 if(Input.GetMouseButtonDown(0))  
 {
 
 var hit : RaycastHit;
 
 // Cast a ray
 
 if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
 {
 hitObject = hit.collider.gameObject;
 hitObject.transform.parent = gameObject.transform;
 }
 }
 }
 
 if(Input.GetButtonDown("mouse 1")) // This will release the object 
 {
 hitObject.transform.parent = null;
 hitObject = null;
 }

i get a few error's and i know it is probably placed wrong. so how do i adjust it right to the stuff i want? also with yours i got the error when i put mouse as the release button is still had to place something there.

Comment
Add comment · Show 2 · 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 BarkShark · Dec 18, 2011 at 01:27 PM 0
Share

Please format your code, just edit your answers, select you code and hit the '101 010' button

avatar image artsdcs · Dec 18, 2011 at 05:54 PM 0
Share

tnx i did that

avatar image
0

Answer by BarkShark · Dec 18, 2011 at 06:44 PM

Hmm, you still make a couple of basic mistakes, an if statement has to go in '()'. For example :

 if(condition is true)
 {
 do something;
 }


This is the full code to do what you want:

 var hitObject : GameObject;
 
 function Update()
 {
     if(Input.GetMouseButtonDown(0))  
     {
     
     var hit : RaycastHit;
     
     // Cast a ray
     
     if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
     {
     
     if(hitObject.collider.tag == "Pick")
     {
     
     hitObject = hit.collider.gameObject;
     hitObject.transform.parent = gameObject.transform;
     }
     }
     }
     }
     
     if(Input.GetMouseButtonUp(0)&& hitObject != null) // This will release the object 
     {
     hitObject.transform.parent = null;
     hitObject = null;
     }
     }
Comment
Add comment · Show 1 · 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 artsdcs · Dec 18, 2011 at 08:43 PM 0
Share

got an error becuse of the last } so i removed it and i got to play but i could not pick op the cube with the Pick tag so i added the cube to the hit object and now i can still pickup everything like the wall and ground and i can't release it. and also if it finally works so i can only pick up objects that are like 5 meter away from the middle from the camera will that to go trough the wall to?

thank you so far

avatar image
0

Answer by psychentist · Apr 30, 2012 at 02:08 AM

I wanted to do something similar, but FAIR WARNING, Even if you get this script working, It won't act like amnesia. Unity STILL has issues getting objects to obey scripts and physics at the same time. Either you will have a rigidbody on the object, which will cause it to fall unless you are actively moving it upwards, or you will have gravity off, which will cause it to fly into space when the object touches the ground, or you will not have a rigidbody, and the object will not obey normal physics, but will be a slave to the script, allowing it to pass through the floor and such. If you really want, I could upload the script I used, but IDK a workaround for the issues involved.

Comment
Add comment · Show 2 · 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 exvalid · Oct 20, 2017 at 07:46 PM 0
Share

$$anonymous$$ove the object from the object should solve this problem.

avatar image exvalid · Oct 20, 2017 at 07:47 PM 0
Share

i used 2 scripts to do it and a very selective call prossess.

  • 1
  • 2
  • ›

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

11 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

Related Questions

Drag Rigidbody that respects collisions. 1 Answer

My dragrigidbodies script makes the carried object jittery 0 Answers

Drag around riggidbodies with a "pin" 0 Answers

How to move a group of objects with rigidbodies together? 2 Answers

How can i set AddRelativeForce relative to how fast/slow im moving my mouse? 2 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