Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 lingai · Jul 13, 2012 at 06:56 AM · c#movementgamemouseswipe

Mouse Swipe and Code Efficiency

Ok so Im trying to make this bottle appear at the beginning of the game and slide on screen from the left side(2D btw) and move to the center and stop. From there the player swipes their mouse across the bottle top, from right to left, which pops the top off sliding it to the left and then deleting itself. After the bottlecap is gone, and only when its gone, is the player then able to lift the bottle up by swiping it upward having it stop at a certain point.

I got my code to do all of this rather clunkishly and really inefficiently do to my lack of Unity Knowledge and would love if someone could help make it more efficient and explain how they did that so I can actually learn from this and better by code :D

It's kind of alot sorry If I made the post too long and/or couldve left some stuff out.

so here's the bottle move on screen code(which is attacked the camera)

 public class BottleSummon : MonoBehaviour 
 { 
 
      public GameObject BottlePrefab;
      void Start () 
      {
           Vector3 position = new Vector3(6,-3,0);
           Instantiate(BottlePrefab, position, Quaternion.identity); 
      }
 }
 
 
 and here is the bottle move on screen code(which is attached to the bottle)
 
 
 public class BottleMoveonScreen : MonoBehaviour
 {
  
      public float BottleSpeed;
  
      void Update() 
      {
          float amtToMove = BottleSpeed * Time.deltaTime;
  
          transform.Translate(Vector3.left * amtToMove);
  
  
         if(transform.position.x <= 0)
         {
              transform.position = new Vector3(0, transform.position.y, transform.position.z); 
  
         }
  
        if(transform.position.x >= 0)
        {
              transform.position = new Vector3(0, transform.position.y, transform.position.z);
        }
  
        if(transform.position.y <= -3)
        {
              transform.position = new Vector3(transform.position.x, -3, transform.position.z);
  
        }
  
       if(transform.position.y >=-1)
       {
              transform.Translate(Vector3.up);
  
              if(transform.position.y >=1.5)
              {
                  transform.position = new   Vector3(transform.position.x,1,transform.position.z);
              }
       }
    }
 }
 
 
 here is my bottlecap swiping code (attached to the bottle cap
 
 public class MouseDrag : MonoBehaviour
 {
      public static bool Gone = false;
  
      Vector3 myPosition;
  
      void OnMouseOver()
      {
  
           OnMouseDrag(); 
      }
 
      void OnMouseDrag()
      {
           Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  
           transform.position = new Vector3(clickedPosition.x, -1.8f,0);
  
           if(clickedPosition.x >= transform.position.x)
           {
  
               transform.position = new Vector3(transform.position.x,transform.position.y,transform.position.z); 
           }
  
           if(transform.position.x >= 0)
           {
               transform.position = new Vector3(0, transform.position.y, transform.position.z);
  
           }
  
  
           if(transform.position.x <=-1.5)
           {
                Destroy(gameObject);
                Gone = true;
                Debug.Log("True");
           }
  
       
     }
 }
 
  
 and Lastly my bottle lifting code(attached to the bottle)
 
 public class BottleLift : MonoBehaviour 
 {
  
      public GameObject BottleCap;
  
  
      void OnMouseOver()
      {
          if(MouseDrag.Gone = true)
          {
              OnMouseDrag(); 
          } 
      }
 
      void OnMouseDrag()
      {
  
          Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
          transform.position = new Vector3(0,clickedPosition.y,0);
  
      }
  
 }
Comment
Add comment · Show 5
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 whydoidoit · Jul 13, 2012 at 07:10 AM 0
Share

Please format your code by indenting it 4 spaces or 1 tab before pasting, or by highlighting it and clicking the code button after pasting. I've done my best to format it this time, but some of the symbols have been changed to text representations.

avatar image lingai · Jul 13, 2012 at 07:45 AM 0
Share

I'm sorry I did code it by tabing spaces, and I even took out unneeded lines, but for some reason it posted this way. I could've done something wrong when posting it. Again sorry about that.

avatar image whydoidoit · Jul 13, 2012 at 07:54 AM 0
Share

You need an extra 4 spaces (or 1 tab) for this editor to show it as code. Click the ? above the answer/question box for help on the editor

avatar image lingai · Jul 13, 2012 at 08:33 AM 0
Share

Oh ok thank you I'll mod it

avatar image lingai · Jul 14, 2012 at 04:41 AM 0
Share

Hey thanks alot for your help guys, but I actually figured it out! and condensed my code extremely! it took all day lol, but I got it!

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

How to make my spaceship move by mouse? 2 Answers

How to make the turret of a tank in a 3rd person game look at the mouse cursor? 1 Answer

Distribute terrain in zones 3 Answers

Locking movement to one axis only (3D game) 1 Answer


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