Skip to content

Instantly share code, notes, and snippets.

@sahin52
Created March 28, 2021 09:31
Show Gist options
  • Save sahin52/c95534349837f8f3835c0591716763af to your computer and use it in GitHub Desktop.
Save sahin52/c95534349837f8f3835c0591716763af to your computer and use it in GitHub Desktop.
/**
This script is created by Şahin Kasap (github.com/sahin52)
This script contains an example for smooth and clean dragging when colliders exist on a scene.
Normally, when we just add dragging functionality, even though the colliders exist, the objects passes through colliders
(there becomes a small bumb but it is not enough), so I added the colliders tags, and added the tags to a List.
If the collider is in the list and the mouse position is on the other side of the collider, I make the velocity to the other position 0.
Not a perfect solution but works fine in my case.
*/
using System.Collections.Generic;
using UnityEngine;
class SmoothMouseDragging: MonoBehaviour{
List<string> collideds = new List<string>();
private Rigidbody rb;
void Awake(){
rb = GetComponent<Rigidbody>();
}
void FixedUpdate(){
var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = transform.position.z;
var velocity = mousePos-transform.position;
if(collideds.Contains("borderTop") && velocity.y>0f){
print("cancelling upwards speed");
velocity.y = 0;
}
if(collideds.Contains("borderLeft") && velocity.x<0f){
print("cancelling left speed");
velocity.x = 0;
}
if(collideds.Contains("borderBottom") && velocity.y<0){
print("cancelling downwards spped");
velocity.y=0;
}
rb.velocity = velocity*speed;
}
void OnCollisionEnter(Collision collision){
if(collideds.IndexOf(collision.collider.tag)<0)
collideds.Add(collision.collider.tag);
}
void OnCollisionExit(Collision other){
if(collideds.IndexOf(other.collider.tag)>-1){
collideds.RemoveAt(collideds.IndexOf(other.collider.tag));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment