Hello Guest

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Kevin

Pages: [1]
1
Support / Spot of input.mousePosition and Rotation of sprite
« on: September 10, 2014, 05:10:56 pm »
Hello 2D Toolkit community and Unikron Software, so basically, I want to rotate my characters arm towards the mouse. This hasn't anything to do with 2D Toolkit I guess.

I work with 2D platform controller from JNA Mobile, which is a third party asset bought from the Asset Store. This controller makes my character move and such, and I have made a Character Animation which changes the currently playing animation of the different body parts with for example: frontArm.Play ("walk_frontarm"), this all works well when nothing rotates towards the mouse position. this Animation Bridge also changes the scale of the parent gameObject so it will be mirrored.

The problem I'm facing is that when I move the mouse over the character (which is in the center of the screen), somewhere near the absolute center the arm rotates the wrong way! And stays that way until I change the scale of my character. Here is the rotation code:
Code: [Select]
private void AimDirection () {
if(inventoryAndEquipment.CurrentWeapon != null) {
Vector3 mousePos = Input.mousePosition;
mousePos.x -= Screen.width / 2;
mousePos.y -= Screen.height / 2;

float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
if (angle<0) angle += 360;

if (inventoryAndEquipment.CurrentWeapon is RangedWeapon) {
Quaternion rotation = Quaternion.Euler(0f, 0f, angle);
frontArm.transform.rotation = rotation;
}
}
}

I've tried different things which all end up to the same problem! All help and thoughts will be appreciated and the reason I ask it here is because I've already put a question in Answers from Unity3d, but only 1 person replied to me, which asked a question, so I couldn't find out the problem, this is the link to the Answers post:
http://answers.unity3d.com/questions/787125/negative-scale-and-rotation.html

All help will be appreciated. If you need more information to help me, like screenshots or more code, I see what I can do.
Screenshots will need to be discussed with a friend of mine because we're making the game together, and we both don't want to show anything unfinished, but if there is no other way, we'll provide you with screenshots.

Thanks in advance,
Kevin

So I've googled a for a bit more, and I found this post:
http://2dtoolkit.com/forum/index.php?topic=3370.0
I think I have the same error because enabling and disabling the gameObject seems to work for me too..

2
Hello 2D Toolkit,

I want to create a copy from a sprite sheet including all sprite definitions.
I had this before in another game, but these were copied outside Unity, then I just changed the Spritesheet texture.

But now I want it in the same collection as the first one? How can I best do this? I am trying to make an Editor script. But I have no clue where to start.
I found a CopyFrom method in the script. But I don't know how to use it, because if I add an extra sprite sheet and copy this information, after that change the Texture. I am afraid it will not be saved or corrupt my Object somehow, because there will not be made sprite definitions for the new sheet I guess?

But I am afraid I break anything that has to do with 2D Toolkit, or maybe even my game. (though I made a back-up before I started this)
After this is done I want to create animations for it.

Some info:
- The sprite sheet consists of a front arm, back arm, legs and a male body, a female body and 4 heads.
- Some sprites have attachPoints while others have an different anchor.


So what I want is help with a script which copies the current sprite sheet, including definitions. Then change texture of the sprite sheet. (Maybe an idea for the Editor, a Copy button for sprite sheets)

I want all character sprites in one collection, because it will be called in the same Draw Call, at least that's what I've read. (So there will be better performance);

Currently what I have is this, put in a new scene on main camera, I just run the scene, then stop scene and I have new sprite sheet but not the data:
Code: [Select]
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class CloneSpriteCollections : MonoBehaviour {

public tk2dSpriteCollection BaseCollection;
public Texture2D[] textures;

void Start() {
List<tk2dSpriteSheetSource> spriteSheets = new List<tk2dSpriteSheetSource>();
tk2dSpriteCollectionData data = BaseCollection.spriteCollection;
tk2dSpriteSheetSource[] sources = BaseCollection.spriteSheets;
foreach (var v in spriteSheets)
{
var t = new tk2dSpriteSheetSource();
t.CopyFrom(v);
spriteSheets.Add(t);
}
// Adding the new spritesheets
foreach(Texture2D texture in textures) {
var t = new tk2dSpriteSheetSource();
t.CopyFrom (sources [0]);
t.texture = texture;
spriteSheets.Add(t);
}
BaseCollection.spriteSheets = spriteSheets.ToArray ();


List<tk2dSpriteDefinition> newDefs = new List<tk2dSpriteDefinition>();
tk2dSpriteDefinition[] defs = data.spriteDefinitions;
foreach (tk2dSpriteDefinition def in defs) {
newDefs.Add (def);
}
int defOffset = 0;
foreach (var v in spriteSheets) {
int y = 288;
for (int i = 0; i < y; i++) {
tk2dSpriteDefinition def = defs[i];
def.name = v.Name + "/" + i;
newDefs.Add (def);
}
}
}
}

While i reread my code I found out why, I don't convert the list back to the array! Let me try that first ;)

So now I got this: Though it does not yet add the correct textures, it makes the definitions and stuff, it does not work correctly yet!
Code: [Select]
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;

public class CloneSpriteCollections : MonoBehaviour {

public tk2dSpriteCollection BaseCollection;
public Texture2D[] textures;

void Start() {
List<tk2dSpriteSheetSource> spriteSheets = new List<tk2dSpriteSheetSource>();

tk2dSpriteCollectionData data = BaseCollection.spriteCollection;
tk2dSpriteSheetSource[] sources = BaseCollection.spriteSheets;
foreach (var v in sources)
{
var t = new tk2dSpriteSheetSource();
t.CopyFrom(v);
spriteSheets.Add(t);
}
// Adding the new spritesheets
foreach(Texture2D texture in textures) {
var t = new tk2dSpriteSheetSource();
t.CopyFrom (sources [0]);
t.texture = texture;
spriteSheets.Add(t);
}
BaseCollection.spriteSheets = spriteSheets.ToArray ();

tk2dSpriteCollectionDefinition[] cDefs = BaseCollection.textureParams;
List<tk2dSpriteCollectionDefinition> newCDefs = new List<tk2dSpriteCollectionDefinition>();
foreach (tk2dSpriteCollectionDefinition def in cDefs) {
newCDefs.Add (def);
}
int sheetId = 1;
foreach (var v in spriteSheets) {
int y = 288;
for (int i = 0; i < y; i++) {
tk2dSpriteCollectionDefinition def = cDefs[i];
def.spriteSheetId = sheetId;
def.name = v.Name + "/" + i;
newCDefs.Add (def);
}
sheetId++;
}
BaseCollection.textureParams = newCDefs.ToArray();

List<tk2dSpriteDefinition> newDefs = new List<tk2dSpriteDefinition>();
tk2dSpriteDefinition[] defs = data.spriteDefinitions;
foreach (tk2dSpriteDefinition def in defs) {
newDefs.Add (def);
}

foreach (var v in spriteSheets) {
int y = 288;
for (int i = 0; i < y; i++) {
tk2dSpriteDefinition def = defs[i];
def.name = v.Name + "/" + i;
newDefs.Add (def);
}
}

BaseCollection.spriteCollection.spriteDefinitions = newDefs.ToArray ();
}
}

Pages: [1]