Question by
Vandal16 · May 05, 2021 at 02:53 PM ·
c#hingejointdraggable
Draggable doors problem
Hi, I have doors that has a Hinge Joint and a player that has a script that allows to drag doors like in Amnesia. It work almost fine until the player tries to drag to his side where he is, the doors make really weird movements. How could I avoid that? Did I do something wrong in the Hinge Joint component? Video: https://www.youtube.com/watch?v=Uh20ZIex43A
And Hinge Joint component
obraz-2021-05-05-164911.png
(61.4 kB)
Comment
And the scripts that allows to drag the doors: using UnityEngine;
[System.Serializable]
public class DoorGrabClass
{
public float m_DoorPickupRange = 2f;
public float m_DoorThrow = 10f;
public float m_DoorDistance = 2f;
public float m_DoorMaxGrab = 3f;
}
[System.Serializable]
public class TagsClass
{
public string m_DoorsTag = "Door";
}
public class Door : MonoBehaviour
{
#region Variables
public GameObject playerCam;
public string GrabButton = "Grab";
public DoorGrabClass DoorGrab = new DoorGrabClass();
public TagsClass Tags = new TagsClass();
private float PickupRange = 3f;
private float ThrowStrength = 50f;
private float distance = 3f;
private float maxDistanceGrab = 4f;
private Ray playerAim;
private GameObject objectHeld;
private bool isObjectHeld;
private bool tryPickupObject;
#endregion
private void Start()
{
isObjectHeld = false;
tryPickupObject = false;
objectHeld = null;
}
private void FixedUpdate()
{
if (Input.GetButton(GrabButton))
{
if (!isObjectHeld)
{
tryPickObject();
tryPickupObject = true;
}
else
{
holdObject();
}
}
else if (isObjectHeld)
{
DropObject();
}
}
private void tryPickObject()
{
Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
RaycastHit hit;
if (Physics.Raycast(playerAim, out hit, PickupRange))
{
objectHeld = hit.collider.gameObject;
if (hit.collider.tag == Tags.m_DoorsTag && tryPickupObject)
{
isObjectHeld = true;
objectHeld.GetComponent<Rigidbody>().useGravity = true;
objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
PickupRange = DoorGrab.m_DoorPickupRange;
ThrowStrength = DoorGrab.m_DoorThrow;
distance = DoorGrab.m_DoorDistance;
maxDistanceGrab = DoorGrab.m_DoorMaxGrab;
}
}
}
private void holdObject()
{
Ray playerAim = playerCam.GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
Vector3 nextPos = playerCam.transform.position + playerAim.direction * distance;
Vector3 currPos = objectHeld.transform.position;
objectHeld.GetComponent<Rigidbody>().velocity = (nextPos - currPos) * 10;
if (Vector3.Distance(objectHeld.transform.position, playerCam.transform.position) > maxDistanceGrab)
{
DropObject();
}
}
private void DropObject()
{
isObjectHeld = false;
tryPickupObject = false;
objectHeld.GetComponent<Rigidbody>().useGravity = true;
objectHeld.GetComponent<Rigidbody>().freezeRotation = false;
objectHeld = null;
}
Your answer
Follow this Question
Related Questions
Help with coding crane 0 Answers
Draggable Door Problems 0 Answers
Children of a hingejoint not colliding with parent's mesh when connectedBody is assigned 1 Answer
Rotate a hinge joint on its own 0 Answers