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.

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

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.

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.

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

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.

I posted a script at

I made this myself, tested, 100% works. Promise. I haven’t made the throwing work yet, but I’m working on that now.

using UnityEngine;
using System.Collections;

public class HoldObject : MonoBehaviour {

	float drag = 1.0f;
	float angularDrag = 5.0f;
	bool attachToCenterOfMass = false;
	
	private FixedJoint fixedJoint;
	
	void Update() {
		
		// Make sure the user pressed the mouse down
		if (!Input.GetMouseButtonDown (0))
			return;
	
		var mainCamera = FindCamera();
			
		// We need to actually hit an object
		RaycastHit hit;
		if (!Physics.Raycast(mainCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2)), out hit, 100))
			return;
		
		// We need to hit a rigidbody that is not kinematic
		if (!hit.rigidbody || hit.rigidbody.isKinematic)
			return;
		
		if (!fixedJoint) {
			
			GameObject go = new GameObject("Rigidbody dragger");
			Rigidbody body = go.AddComponent("Rigidbody") as Rigidbody;
			fixedJoint = go.AddComponent("FixedJoint") as FixedJoint;
			body.isKinematic = true;
		}
		
		fixedJoint.transform.position = hit.point;
		if (attachToCenterOfMass) {
			
			Vector3 anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
			anchor = fixedJoint.transform.InverseTransformPoint(anchor);
			fixedJoint.anchor = anchor;
		} else {
			fixedJoint.anchor = Vector3.zero;
		}
		
		fixedJoint.connectedBody = hit.rigidbody;
		
		StartCoroutine("DragObject", hit.distance);
	}
	
	IEnumerator DragObject (float distance) {
		
		float oldDrag = fixedJoint.connectedBody.drag;
		float oldAngularDrag = fixedJoint.connectedBody.angularDrag;
		
		fixedJoint.connectedBody.drag = drag;
		fixedJoint.connectedBody.angularDrag = angularDrag;
		
		Camera mainCamera = FindCamera();
		
		while (Input.GetMouseButton (0)) {
			
			var ray = mainCamera.ScreenPointToRay (new Vector2(Screen.width / 2, Screen.height / 2));
			fixedJoint.transform.position = ray.GetPoint(distance);
			yield return null;
		}
		
		if (fixedJoint.connectedBody) {
			
			fixedJoint.connectedBody.drag = oldDrag;
			fixedJoint.connectedBody.angularDrag = oldAngularDrag;
			fixedJoint.connectedBody = null;
		}
	}
	
	Camera FindCamera () {
		if (camera)
			return camera;
		else
			return Camera.main;
	}
}

Hey I have added 2 Script to Complete a full Grab toggle and ready to move code,

Fixed An Inversion Error and a Euler read error while moving. since yesterday it is now working as stated below

ObjectGrabIdAndLock, ObjectReplyIdAndMove

ObjectGrabIdAndLock goes on Main player,… ObjectReplyIdAndMove goes on Moveable Objects remember to add layers in you want to hit in inspector and pick the Headcam Ect

Updated Scripts Since the other week now with full movement And Rotation and Full Inversion Options

This is Complete bar Diagnals and Mouse Rotation as im adding this now, you want to use the option OverideDiagnals and possibly UseDefaultRotation if u hate my defualt.

too add mouse copy the whole auto inversion and paste it underneath and swap the names to the mouse names instead of the default keys its a mission dont attempt it lol. i will do this over the week as still cleaing up the script its pretty large, Please contact at Exvalid@gmail.com To give me job coding.

cheers Ryan kappeslink text Exvalid@gmail.com[105518-objectgrabscripts.zip|105518]