- Home /
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.
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
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.
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.
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; }
i dont remember but there was somewhere a random { or } probably my fault i dont know. but it still doesn't work properly sry.
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.
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.
Please format your code, just edit your answers, select you code and hit the '101 010' button
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;
}
}
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
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.
$$anonymous$$ove the object from the object should solve this problem.
i used 2 scripts to do it and a very selective call prossess.
Your answer
Follow this Question
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