Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Lukx31 · Dec 06, 2015 at 12:21 AM · 2dcollidermousecollider2dmouse-drag

Object Follow Mouse Without Making The Collider Stops Working

So i have a cube and a sphere, i can move the cube using the mouse, but if i don't move the cube the ball hits the cube and changes the direction(thats what i want to happen), but if i move the cube and put it in front of the ball, the ball just passes trough the cube... Can someone help me??? (in everything im using vector 2 because its a 2d game, but im using 3d objects.

Code(For the cube): using UnityEngine; using System.Collections;

 public class MouseDrag : MonoBehaviour {
     
 
     void OnMouseDrag(){
         Vector2 MousePosition = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
         Vector2 objPosition = Camera.main.ScreenToWorldPoint (MousePosition);
 
         transform.position = objPosition;
     }
 
 
 }

for the ball:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerMovement : MonoBehaviour {
     
     float moveHorizontal = 1;
     float moveVertical = 1;
 
     public float speed;
 
     int TouchRight = 0;
     int touchDown = 0;
     
 
     void FixedUpdate(){
         if (TouchRight == 0 && touchDown == 0) {
             Up ();
         } 
 
         if(TouchRight == 1) 
         {
             Right();
         }
 
         if(touchDown == 1) 
         {
             Down();
         }
     }
 
     void OnTriggerEnter(Collider other){
         if (other.name == "ObstacleRight") {
             TouchRight = 1;
         }
 
         if (other.name == "ObstacleDown") {
             touchDown = 1;
         }
     }
 
 
     void Up(){
 
         Vector2 movement = new Vector2 (0f, moveVertical);
 
         GetComponent<Rigidbody> ().velocity = movement * speed;
     }
 
     void Right(){
 
         Vector2 movement = new Vector2 (moveHorizontal, 0f);
         
         GetComponent<Rigidbody> ().velocity = movement * speed;
 
     }
 
     void Left(){
         Vector2 movement = new Vector2 (moveHorizontal * (-1) , 0f);
         
         GetComponent<Rigidbody> ().velocity = movement * speed;
     
     }
 
     void Down(){
         Vector2 movement = new Vector2 (0f, moveVertical * (-1));
         
         GetComponent<Rigidbody> ().velocity = movement * speed;
         
     }
 }
 
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GingahNinja47 · Dec 24, 2018 at 02:55 PM

@Lukx31 I know this is a very late reply, but considering you don't have any I figured I might as well give you one. It's far from perfect, but this is the solution I used that works well enough:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour
 {
     public float speedMod;
     public float farLeft, farRight, farTop, farBottom;
     private Vector3 prevPos, currPos;
     
     // Update is called once per frame
     void FixedUpdate () {
         if (Input.GetMouseButton(0))
         {
             if (Input.GetMouseButtonDown(0))
             {
                 prevPos = Input.mousePosition;
             }
             currPos = Input.mousePosition;
         }
 
         Vector3 currWorldPos = currPos;
         currWorldPos.z = 0;
         Vector3 prevWorldPos = prevPos;
         prevWorldPos.z = 0;
 
         transform.position = new Vector3(
             Mathf.Clamp(transform.position.x, farLeft, farRight), 
             Mathf.Clamp(transform.position.y, farBottom, farTop), 
             0);
         Vector3 speed = (currWorldPos - prevWorldPos) * speedMod * Time.deltaTime;
         GetComponent<Rigidbody>().velocity = speed;
 
         prevPos = currPos;
     }
 }
 

Basically, rather than directly moving the object's position, you change the velocity on an attached Rigidbody. It takes some finagling with the speedMod to get it to line up just right, as different camera distances require different speeds to allow the object to keep up with the mouse, but it's the only way I've managed to get the colliders to work perfectly.

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.

Follow this Question

Answers Answers and Comments

51 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

Related Questions

Different colliders in prefabs 0 Answers

Trigger Collinder doesnt work with Input.GetKey 1 Answer

2d Tilemap collidor not working 0 Answers

Setting points of edge collider 2D from script 0 Answers

Co ordinates of 2D object/image,Get Co ordinates covered by Object 0 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