- Home /
Can´t attach 2D Bones to the Character Script
I have made a 2DCharakter with 2DBones (Anima2D) and i can´t attach them to the Gameobject in the Character Script because it only requires Sprite renderers. The Character Script allows the 2D player to rotate his Arms like in those 2D sidescroll shooter games and all I want to do is to attach those Bones (which are attached to the ArmSprites) to the first Hand, but I can only attach normal Sprites. This is the Character Script:
public class Character : MonoBehaviour, IDamageable {
[SerializeField] Animator HandsAnimator; //Hands animator for management hands and guns animation
[SerializeField] SpriteRenderer HeadTransform; //To control the position and rotation of the head
[SerializeField] Transform HandsTransform; //To control the position and rotation of the hands
[SerializeField] Transform ShotStartTransform; //To determine the start of the shot
[SerializeField] float HandsOnDeathRotation; //Hack, for the position of the hands at death, you can do in the animator.
[SerializeField] float HeadOnDeathRotation; //Hack
[SerializeField] float DeathTime; //Hack
[SerializeField] float DestroyCharacterAfterDeathTime;
[SerializeField] Transform CameraTargetPoint; //The point behind which the camera will follow
[SerializeField] Renderer MainRenderer; //For autoaim and AILogic
//Parts of the hand, for modification possibilities
[Header("FirstHand")]
[SerializeField] SpriteRenderer FirstShoulder;
[SerializeField] SpriteRenderer FirstForearm;
[SerializeField] SpriteRenderer FirstForearmOutline;
//Parts of the hand, for modification possibilities
[Header("SecondHand")]
[SerializeField] SpriteRenderer SecondShoulder;
[SerializeField] SpriteRenderer SecondForearm;
[SerializeField] SpriteRenderer SecondForearmOutline;
//Sounds
[Space(10),Header("Sounds")]
[SerializeField] AudioClipPreset JumpSound;
[SerializeField] AudioClipPreset StepSound;
private int GroundCollided; //Touch counter to ground
private int CurrentWeaponIndex; //Index weapon in hands
private List<WeaponEntity> Weapons = new List<WeaponEntity>(); //Weapons list
private Loot LootInInteractionZone; //Not null if character is near loot
private WeaponEntity selectedWeapon; //Weapon in hands
private HashSet<Collider2D> TriggeredObjects = new HashSet<Collider2D>(); //To control the re-trigger
private Collider2D LastColision; //For stop move animation on dynamic props
#region Public properties
public CharacterDescription Description { get; private set; }
public bool IsUserControlled { get { return Input == InputController.Instance.CurrentController; } }
public bool IsBot { get { return Input is AIController; } }
public GamePanel GamePanel { get { return GamePanel.Instance; } }
public Rigidbody2D RB { get; private set; }
public Animator Animator { get; private set; }
public Vector2 Position { get { return transform.position; } }
public Transform GetTransform { get { return transform; } }
public Renderer GetRenderer { get { return MainRenderer; } }
public int DirectionByX { get; private set; }
public Animator GetHandsAnimator { get { return HandsAnimator; } }
public WeaponEntity SelectedWeapon {
get {
return selectedWeapon;
}
private set {
if (selectedWeapon != null) {
selectedWeapon.DeselectWeapon();
}
selectedWeapon = value;
selectedWeapon.SelectWeapon(this);
}
}
public LayerMask EnemyMask {
get {
if (B.Layers.AlliesMask.LayerInMask(gameObject.layer)) {
return B.Layers.EnemyMask;
} else {
return B.Layers.AlliesMask;
}
}
}
public bool InAir { get { return GroundCollided == 0; } }
public Transform GetShotStartTransform { get { return ShotStartTransform; } }
public float Health { get; private set; }
public bool IsDead { get; private set; }
InputBaseClass Input { get; set; }
#endregion //Public properties
#region Private properties
private LayerMask GroundMask { get { return B.Layers.GroundMask; } }
private LayerMask LootMask { get { return B.Layers.LootMask; } }
private Vector2 DirectionToAim { get { return Input.AimPos - (Vector2)transform.position; } }
#endregion //Private properties
#region Instantiation
/// <summary> Create and initialisation character </summary>
/// <param name="spawnPoint"> Spawn character position </param>
/// <param name="description"> Character description</param>
public static Character CreateCharacter (Vector3 spawnPoint, CharacterDescription description) {
var newCharacter = Instantiate(description.CharacterPrefab);
newCharacter.transform.position = spawnPoint;
newCharacter.InitCharacter(description);
return newCharacter;
}
/// <summary> Initialisation character after start </summary>
/// <param name="description"> Character description</param>
private void InitCharacter (CharacterDescription description) {
Description = description;
Health = Description.MaxHealth;
RB = GetComponent<Rigidbody2D>();
Animator = GetComponent<Animator>();
SelectedWeapon = new WeaponEntity(this, Description.StartedWeapon);
SelectedWeapon.CartridgesTotal = SelectedWeapon.Weapon.MaxCartridges;
Weapons.Add(SelectedWeapon);
if (RB == null) { Debug.LogError("RigidBody2D not found", this); }
if (Animator == null) { Debug.LogError("Animator not found", this); }
Animator.runtimeAnimatorController = Description.Animator;
//Set hands
FirstShoulder.sprite = Description.FirstShoulder;
FirstForearm.sprite = Description.FirstForearm;
FirstForearmOutline.sprite = Description.FirstForearmOutline;
SecondShoulder.sprite = Description.SecondShoulder;
SecondForearm.sprite = Description.SecondForearm;
SecondForearmOutline.sprite = Description.SecondForearmOutline;
//Set head
HeadTransform.sprite = Description.HeadSprite;
}
What do i have to Change in the Character Script in Line 24 and line 129?
Answer by cdr9042 · Jan 04, 2019 at 02:54 AM
You have to use SpriteMeshRenderer (Created by 2D Object > Sprite Mesh). To have a sprite mesh, right click on your sprite texture, then Create > Anima2D > Sprite Mesh. Then attach the bone to your sprite mesh by first having that SpriteMeshRenderer in the scene, then open the SpriteMesh Editor and bind the bone.
Your answer
Follow this Question
Related Questions
Sprite managed by Sprite Renderer doesn't shows up in play mode 0 Answers
2D game camera background between sprites 0 Answers
Unity 2D: query for "mask/layer" bit on screen space without collider 1 Answer
How can my sprite move to new direction it's facing not keep going to old direction? 0 Answers
How do I get the sprite pointing the right direction? 1 Answer