• 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 JackJack86 · Jun 07, 2019 at 11:42 AM · mouseclick

Strange behavior at OnMouseUp()/OnMouseDown

I'm currently working on an Angry Birds type project for School on 2019.1.0f2 and am running into an issue where my projectile is transported to the camera position. I've narrowed the issue down to the OnMouseUp() function being called before actually lifting up on the mouse button but haven't a clue how to fix this. I get the same behavior on 2018.3.7f1

 private void OnMouseDown()
 {
     spring.enabled = false;
     clickedOn = true;
 }

 private void OnMouseUp()
 {
     spring.enabled = true;
     rb2d.isKinematic = false;
     clickedOn = false;
 }
Comment
Add comment · Show 3
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 Doctor_ED · Jun 07, 2019 at 12:01 PM 0
Share

@JackJack86 Can you tell (paste the code) about what happens in between those 2 events?

avatar image Magso · Jun 07, 2019 at 12:42 PM 0
Share

The code you have posted doesn't have any errors, you're just setting variables. What/where are you using these variables for?

avatar image JackJack86 · Jun 08, 2019 at 05:12 AM 0
Share

here's the rest of the code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ProjectileDragging : $$anonymous$$onoBehaviour { public LineRenderer catapultLineFront; public LineRenderer catapultLineBack;

 private Ray leftCatapultToProjectileRay;
 private SpringJoint2D spring;
 private Transform rock;
 private float circleRadius;
 private bool clickedOn;
 private Rigidbody2D rb2d;
 private CircleCollider2D circle;
 private Vector2 prevVelocity;
 private float originCatapultX = -13.88f;
 private float originCatapultY = -6.7f;
 private bool mouseButtonUp = false;

 private void Awake()
 {
     spring = GetComponent<SpringJoint2D>();
     rb2d = GetComponent<Rigidbody2D>();
     rock = GetComponent<Transform>();
     circle = GetComponent<CircleCollider2D>();
     catapultLineFront.width$$anonymous$$ultiplier = 0.6f;
     catapultLineBack.width$$anonymous$$ultiplier = 0.6f;
 }

 // Start is called before the first frame update
 void Start()
 {
     LineRendererSetup();
     leftCatapultToProjectileRay = new Ray(catapultLineFront.transform.position, Vector3.zero);
     circleRadius = circle.radius;
 }

 // Update is called once per frame
 void Update()
 {
     if (clickedOn)
     {
         Dragging();
     }
     if (spring != null)
     {
         LineRendererUpdate();
     }
     else
     {
         catapultLineFront.enabled = false;
         catapultLineBack.enabled = false;
     }
 }

 private void On$$anonymous$$ouseDown()
 {
     spring.enabled = false;
     clickedOn = true;
 }

 private void On$$anonymous$$ouseUp()
 {
     spring.enabled = true;
     rb2d.is$$anonymous$$inematic = false;
     clickedOn = false;
 }

 void Dragging()
 {
     Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     Vector2 catapultTo$$anonymous$$ouse = mouseWorldPoint - new Vector3(originCatapultX, originCatapultY, 0);
     mouseWorldPoint.z = 0f;
     transform.position = mouseWorldPoint;
 }

 void LineRendererSetup()
 {
     catapultLineFront.sortingLayerName = "ForeGround";
     catapultLineBack.sortingLayerName = "ForeGround";

     catapultLineFront.sortingOrder = 3;
     catapultLineBack.sortingOrder = 1;
 }

 void LineRendererUpdate()
 {
     Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
     leftCatapultToProjectileRay.direction = catapultToProjectile;
     Vector3 holdPoint = leftCatapultToProjectileRay.GetPoint(catapultToProjectile.magnitude + circleRadius);

     catapultLineFront.SetPosition(1, holdPoint);
     catapultLineBack.SetPosition(1, holdPoint);
 }

}

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.

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

108 People are following this question.

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

Related Questions

Can I change GUIButton behaviour? 1 Answer

Convert vector in world to mesh 1 Answer

Remove a part of image on mouse click ? 1 Answer

Activate gameobject upon mouseclick 1 Answer

mouse Down+Hold and double click events 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