Im making a little script to attach objects (weapons and stuff) to the player’s hand but I want also to add a position and rotation offset so the I can adjust the position and rotation of the object in the component. I’ve been messing around with it but not luck.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Rayco's scripts/Weapon placement")]
public class Weaponparenting : MonoBehaviour {
//This is to select the hand bone
public GameObject HandBone;
public Vector3 offset;
// Use this for initialization
void Start () {
transform.position = HandBone.transform.position;
transform.parent = HandBone.transform;
}
// Update is called once per frame
void Update () {
}
}
My guess is I have to add the offset to the Handbone.transform.position but I suck at this.
Ok I figured out the rotation offset.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Rayco's scripts/Weapon placement")]
public class Weaponparenting : MonoBehaviour {
//This is to select the hand bone
public GameObject HandBone;
public Vector3 RotationOffset;
// Use this for initialization
void Start () {
transform.localEulerAngles = RotationOffset;
transform.position = HandBone.transform.position;
transform.parent = HandBone.transform;
}
// Update is called once per frame
void Update () {
}
}
Nevermind I just realized I needed to use Localposition.
Here is the complete code in case anyone needs it, you just need to attach this to the weapon.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Rayco's scripts/Weapon placement")]
public class Weaponparenting : MonoBehaviour {
//This is to select the hand bone
public GameObject HandBone;
//Objects offsets
public Vector3 RotationOffset;
public Vector3 PositionOffset;
// Use this for initialization
void Start () {
transform.localEulerAngles = RotationOffset;
transform.position = HandBone.transform.position;
transform.parent = HandBone.transform;
transform.localPosition = PositionOffset;
}
}