• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chona1171 · Sep 11, 2012 at 02:42 PM · 2dtexture

2D Gui Textures movement

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BlackHunter · Sep 11, 2012 at 07:12 PM

 add " } "
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chona1171 · Sep 12, 2012 at 06:40 AM 0
Share

$$anonymous$$y problem is not that its not compiling plz read the code , I am getting a non consistent speed.

avatar image
0

Answer by Chona1171 · Sep 12, 2012 at 09:53 PM

I have been able to solve it myself

The problem was in the bit where i was trying to calculate the speeds relative to each other instead of calculating the distance and calculating the relativity between the x and y I was trying to calculate it from each other

 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;
     
             float dist = Mathf.Sqrt((x*x)+(y*y));
             yRelation = y / dist;
             xRelation = x /dist;
             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);
    
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i make a 2D texture Pops up when a player went through the collider? 1 Answer

Unity for 2D (isometric) pixel art game - how to keep the real size of the sprite in-game? 1 Answer

About memory problems on Android 0 Answers

Dynamically setting sprite frames through code - Unity 2D 4.3 0 Answers

Getting the 2d coordinates of an object 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges