- Home /
Vector3 and Quaternion issues.
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;
}
Why aren't you just using some sort of universal hand slot?
You basically place an empty GameObject childed to the players hand, then set the equipped weapons to the objects position and rotation.
You may also need to place your weapons inside empty GameObjects so that their standard 0,0,0 position is actually where the Hand;e would be sitting in the proper position of the hand slot.
Also if you are childing these objects to anything at all, then position and rotation no longer work as intended, you must use localPosition and localRotation so that they are only altered to their new world space and not independently.
Yeah - my gut feeling is that this could all be completely replaced with:
CarryableObject.transform.SetParent(Character.transform);
Answer by leech54 · May 18, 2017 at 11:03 AM
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(...).
Answer by Trevdevs · May 15, 2017 at 05:22 PM
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;
}
}
}
}
}
You probably want to set the localPosition to zero and not the world position of the object. At the moment you move the item to the world origin.
Your answer
Follow this Question
Related Questions
How to add 2 Quaternions. 2 Answers
calculating looking angle between 2 transforms 0 Answers
Rotate player (rigidbody) towards his movement 2 Answers
Pose.ctor - create pose from vector3 and quaternion 1 Answer
Slerp look at direction not working... 3 Answers