- Home /
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);
}
}
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.
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.
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
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!