How would it be possible to create a script that will hold an item, keeping it a fair distance from the player, and moving it around as the mouse moves? Can someone help me?
This is not a request for labor, merely me seeking guidance.
Thanks in advance!
Slayer29179’s way would be the more straightforward way to do it indeed. I suggest taking a look at Physics.Raycast to determine the object you want to pick up, and then use the raycastHit data to see if it can be picked up. If so, change its parent to be the camera and make it get dropped once the player lets go of E.
Here is an example script that should be attached to the camera:
var hit:RaycastHit;
var pickedUpObject:GameObject;
if(Input.GetKey("e")){
if(Physics.Raycast(transform.position,transform.forward,hit,100)){//the order of the parameters might be wrong.
if(hit.gameObject.tag=="DynamicObject"){ //i used a tag to see whether the object cn be picked up, you can use another method that may suit you better
pickedUpObject=hit.collider.gameObject; //we use this to determine whether an object is picked up by the player. If it's not null, then the player is doing so.
hit.gameObject.transform.parent=transform; //attach the object to the camera so it moves along with it.
hit.gameObject.transform.position=transform.position-transform.forward; //might need changing as it's untested.
}
}
}
else if(pickedUpObject!=null){ //if player is not holding E but was picking up an object last frame
pickedUpObject.transform.parent=null; //drop the object
pickedUpObject=null; //and nullify the object pointer
}
However, the method to carry it with the camera may lead to some unwanted results such as it clipping through walls. You could either use an additional linecast to determine its position or you could poke around the DragRigidbody.js script and use its method of carrying for a more elegant solution.
This could be done by changing the parent of the object you get to the camera.
Which is done by object.transform.parent = objectName;
For the collection you could either have a trigger or a distance detection
Hope this helps
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[105514-objectgrabscripts.zip|105514]