Question by
StretchCaleb101 · Jul 29, 2021 at 11:11 AM ·
unity input
Need help converting this script to work with the new input system
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PickUp : MonoBehaviour {
public float pickUpRange=5;
public float moveForce = 250;
public Transform holdParent;
private GameObject heldObj;
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
if (heldObj == null)
{
RaycastHit hit;
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward),out hit, pickUpRange))
{
PickupObject(hit.transform.gameObject);
}
}
else
{
DropObject();
}
}
if(heldObj != null)
{
MoveObject();
}
}
void MoveObject()
{
if(Vector3.Distance(heldObj.transform.position, holdParent.position) > 0.1f)
{
Vector3 moveDirection = (holdParent.position - heldObj.transform.position);
heldObj.GetComponent<Rigidbody>().AddForce(moveDirection * moveForce);
}
}
void PickupObject(GameObject pickObj)
{
if(pickObj.GetComponent<Rigidbody>())
{
Rigidbody objRig = pickObj.GetComponent<Rigidbody>();
objRig.useGravity = false;
objRig.drag = 10;
objRig.transform.parent = holdParent;
heldObj = pickObj;
}
}
void DropObject()
{
Rigidbody heldRig = heldObj.GetComponent<Rigidbody>();
heldRig.GetComponent<Rigidbody>().useGravity = true;
heldObj.transform.parent = null;
heldObj = null;
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613040930im_/https://answers.unity.com/themes/thub/images/avi.jpg)