• 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 WWaldo · Apr 20, 2011 at 04:51 PM · collisiononmousedown

OnMouseDown randomly stopping

Hey everyone,

I am working on a project for someone, and I ran across this issue, and it isn't making a lot of sense to me. I am moving an object around with my mouse, basically a click and drag thing. It will work when it is in the middle area of the field, but if it gets to close to the corners it just stops responding to clicks, so I pause it, move it to the middle area again, and it works fine. Any idea what might cause this weird error? The moving object has a box collider, mesh renderer and rigidbody. I would guess the script isn't important here, but I will post it anyways.

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

public class Paddle : MonoBehaviour {

public float moveSpeed = 0; public RaycastHit hit; public Ray ray; public Vector3 newpos; public Vector3 currentPosition; public bool stateMove = false; public bool first = false; public int index = 0; public int iterate = 0; public Vector3[] holder = new Vector3[50]; public List<Vector3> list = new List<Vector3>();

void Start () { hit = new RaycastHit(); newpos = transform.position; currentPosition = transform.position; }

void Update () { if(stateMove) { if(iterate != 0) { list.Clear(); } iterate = 0; ray = Camera.main.ScreenPointToRay(Input.mousePosition); currentPosition = transform.position;

     if (Physics.Raycast(ray, out hit, 1000.0f)) {
         newpos = new Vector3(hit.point.x, 0.54f, hit.point.z);
     }
     list.Add(newpos);
 }
 else
 {
     if(iterate &lt;  holder.Length &amp;&amp; holder[iterate] != new Vector3(0,0,0))
     {
         moveSpeed += Time.deltaTime;
         transform.position = Vector3.MoveTowards(currentPosition, holder[iterate], moveSpeed*5);
         if(holder[iterate] == transform.position)
         {
             currentPosition = transform.position;
             iterate++;
             moveSpeed = 0;
             //first = false;
         }
     }
 }

}

void OnCollisionEnter(Collision collision){ if(collision.gameObject.name == "CubeHorizontal" || collision.gameObject.name == "Cube"){

 }

} void OnMouseDown() { BreakoutGame.SP.SetMovementState(); stateMove = true; } void OnMouseUp() { BreakoutGame.SP.OffMovementState(); stateMove = false; holder = list.ToArray();

}

}

Any ideas?

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 Paul 7 · Apr 20, 2011 at 08:54 PM

First thing I would do is narrow it down to where the bug could be. Add some Debug.Log() to OnMouseDown() and OnMouseUp(). That will determine if there is a problem in the input or your update function. My guess is in the update. I would then log out your list positions in your on mouse up.

 for(int i = 0; i < holder.Length; i++)
 {
        Debug.Log(holder[i]);
 }

Hope this helps. Just keep narrowing it down till you find the culprit.

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
avatar image
0

Answer by burgunfaust · Apr 20, 2011 at 06:14 PM

Do you have any other collider that doesn't have a renderer, maybe to control bounds. If so it might be getting in the way near the edges of the screen.

Gonna look at the script next.

Edit:

The problem seems to coming from this:

if (Physics.Raycast(ray, out hit, 1000.0f))
{
newpos = new Vector3(hit.point.x, 0.54f, hit.point.z);
}

It doesn't seem like it can get very far from the object. The value just stops.

I would honestly try a different approach than this. A move to click or follow the mouse while the button is down type of thing.

Comment
Add comment · Show 9 · 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 WWaldo · Apr 20, 2011 at 07:54 PM 0
Share

I do, but it isn't getting close enough to be overlapping. That was my thought originally though.

avatar image burgunfaust · Apr 20, 2011 at 10:06 PM 0
Share

Can you post the following:

BreakoutGame.SP.Set$$anonymous$$ovementState();

BreakoutGame.SP.Off$$anonymous$$ovementState();

avatar image WWaldo · Apr 21, 2011 at 06:49 PM 0
Share

Um, sure, but all they do is print something in the UI

avatar image burgunfaust · Apr 21, 2011 at 07:43 PM 0
Share

Okay then, don't bother. Do they work properly?

avatar image WWaldo · Apr 21, 2011 at 08:50 PM 0
Share

They work until the unity is in one of the spots that causes it to not move, then they don't respond either. That was kind of why i put them there, psuedo log i guess

Show more comments

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

No one has followed this question yet.

Related Questions

OnMouseDown() won't work unless gameObject's 2DBox Collider - ‘Is Trigger’ is toggled on/off mid-game in editor. 0 Answers

OnDestroy collision detect 4 Answers

OnMouseDown not called on object for brief period after object moves 1 Answer

help with script 3 Answers

Passing OnMouseDown to other Colliders 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