- Home /
Question by
StretchCaleb101 · Jul 29, 2021 at 08:56 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
Follow this Question
Related Questions
How can I add an Input Axis from a script, input coming from an external program? 0 Answers
2/3 times pressing a button does nothing. 1 Answer
how does unity input works for pc and android 2 Answers
Android uses Bluetooth keyboard as input 0 Answers
Unity runtime coordinate system is different than while developing ? 1 Answer