Hi everybody
All this time working with unity 3d has made me quite lazy and working with 3d objects is so easy and now my lack of math skills have caught up with me.
What i am trying to do is shoot a 2d texture (ball) in the direction of when my mouse is clicking , problem is my results are quite erratic (varying speeds) direction is fine.
I have to use the pixel inset
The ball needs to shoot in the direction of where the mouse is clicking meaning it has to move past that point where i clicked
public class Gem : MonoBehaviour {
public bool IsEmpty = false;
public bool IsShootPoint = false;
public GUITexture textureComponent = null;
public Vector2 movetoPoint;
public Vector2 currentPoint;
public bool IsMoving = false;
public const float speed = 20f;
public float xRelation = 1f;
public float yRelation = 1f;
// Use this for initialization
void Start () {
textureComponent = this.GetComponent(typeof(GUITexture)) as GUITexture;
currentPoint = new Vector2(textureComponent.pixelInset.x,textureComponent.pixelInset.y);
}
// Update is called once per frame
void Update () {
if(IsShootPoint && Input.GetMouseButton(0))
{
//validate mouse bounds
Vector3 mousepos = Input.mousePosition;
movetoPoint = new Vector2(mousepos.x,mousepos.y);
float x = mousepos.x - currentPoint.x;
float y = mousepos.y - currentPoint.y;
if(x < 0){
xRelation = -1;
x = x*-1;
}
yRelation = y/x;
IsMoving = true;
}
if(IsMoving)
{
float speedy = speed * Time.deltaTime;
float x = textureComponent.pixelInset.x + xRelation*speedy;
float y = textureComponent.pixelInset.y + yRelation*speedy;
textureComponent.pixelInset = new Rect(x,y,40f,40f);
}
}
public void SetPixelInset(float x, float y)
{
if(textureComponent == null){
textureComponent = this.GetComponent(typeof(GUITexture)) as GUITexture;
}
textureComponent.pixelInset = new Rect(x,y,40f,40f);
currentPoint = new Vector2(x,y);
}
}
If anyone can help i would greatly appriciate