For some reason my quaternions and vector3s for my picking up and holding items script aern’t working. Would you guys please be so kind as to help me with fixing this?
My declarations:
private Vector3 CharVec;
private Vector3 KnifeVec;
private Vector3 CleaverVec;
private Vector3 BreadKnifeVec;
private Quaternion CharQua;
private Quaternion KnifeQua;
private Quaternion CleaverQua;
private Quaternion BreadKnifeQua;
My values:
CharVec = Character.transform.position;
KnifeVec = new Vector3(1.68f, 3.05f, 2.12f);
CleaverVec = new Vector3(1.68f, 2.61f, 1.95f);
BreadKnifeVec = new Vector3(1.59f, 2.62f, 1.90f);
CharQua = Character.transform.rotation;
KnifeQua = new Quaternion(13.80f, -23.77f, -81.62f, 1f);
CleaverQua = new Quaternion(17.86f, -42.52f, -92.53f, 1f);
BreadKnifeQua = new Quaternion(-17.16f, 167.89f, 90.24f, 1f);
And my usages:
if(CarryableObject.name == "knife"){
CarryableObject.transform.rotation = CharQua * KnifeQua;
CarryableObject.transform.position = CharVec + KnifeVec;
}
if(CarryableObject.name == "cleaver"){
CarryableObject.transform.rotation = CharQua * CleaverQua;
CarryableObject.transform.position = CharVec + CleaverVec;
}
if(CarryableObject.name == "bread knife"){
CarryableObject.transform.rotation = CharQua * BreadKnifeQua;
CarryableObject.transform.position = CharVec + BreadKnifeVec;
}
Quaternions are values between -1.0 and 1.0 it looks like you are trying to give it euler angles. I think the fix might be Quaternion.Euler(…).
From what I understand your trying to make a script that places an item in your hand and you want to pick it up from the ground? I assume here is a simple script that does that. I have not tested if this works there may be some syntax errors I did this completly without auto completion and on notepad++ I will test it and edit it when I am home if it doesn’t work. I did this in 10 minutes so its not perfect but should give you an idea at least.
My script should picks up any item when you left click while looking at it and overrides what it in your hand if you try picking up a new item;
public class PickupItem : MonoBehaviour
{
public Transform hand; //The location of the hand, could use Vector3 instead of Transform but I like doing it this way.
public GameObject itemInHand; //A nice reference to the item you picked up.
void Update()
{
//What I'm gonna do is draw a raycast from the middle of the screen aka when you look at an object. and if you click while looking it will pick up the object.
RaycastHit hit;
Ray ray = Camera.main.ViewportPointToRay (Vector3(0.5f, 0.5f, 0));
if(Physics.Raycast(ray, out hit)
{
if(Input.GetMouseButtonDown(0))
{
Debug.Log(hit.collider.transform.name);
if(hit.collider.tag == "Item"); //MAKE SURE TO NOW TAG any items you want to pickup with a new tag called Item <---- I forgot to do this a lot.
{
if(itemInHand == null) //if no item is in hand pickup what your looking at.
{
hit.collider.transform.SetParent(hand);
itemInHand = hit.collider.transform.gameObject;
itemInHand.position = Vector3.zero;
}
else //if an item is in the hand
{
hand.DetachChildren();
hit.collider.transform.SetParent(hand);
itemInHand = hit.collider.transform.gameObject;
itemInHand.position = Vector3.zero;
}
}
}
}
}