Search Unity

Imported FBX without pass through?

Discussion in 'Physics' started by Slyrfecso1, Jul 11, 2015.

  1. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Hi,

    I have tried to make interior with some furnitures and I would like to move them everywhere.
    The problem is all object are going pass through on each other.
    I tried these:

    - mesh collider (Convex, Is Trigger and without)
    - box collider (Convex, Is Trigger and without, Physic material and without)
    - rigidbody with, and without (with gravity and Is Kinematic and without)

    With rigidbody is better, but I don't need realistic physic.
    Any Idea?
    Thx.

    http://www.csongorfekete.com/Unity_web_player/Terberendezo_v1.html
     
  2. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(BoxCollider))]
    5.  
    6. public class drag_mouse : MonoBehaviour
    7. {
    8.    
    9.     private Vector3 screenPoint;
    10.     private Vector3 offset;
    11.  
    12.     void OnMouseDown()
    13.     {
    14.         screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
    15.  
    16.         offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    17.     }
    18.    
    19.     void OnMouseDrag()
    20.     {
    21.         if(Input.GetKeyDown(KeyCode.A))
    22.         transform.Rotate(0, 90, 0);
    23.  
    24.         float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
    25.         Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen ));
    26.         transform.position = new Vector3( pos_move.x, transform.position.y, pos_move.z );
    27.        
    28.     }
    29.  
    30.  
    31. }
     
  3. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    I tried with simple cube. I would like to move them with mouse without pass through.
    On the next I need snaps to objects...
    Any Idea?

    Thanks.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    That's because the way you move them, if you set it's position directly, then it's not going to respect collision.
     
  5. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    Unfortunately I can't give directly, because I need to move any very in the room with mouse, but You have localise the problem. Thx.
     
  6. Slyrfecso1

    Slyrfecso1

    Joined:
    Jul 16, 2012
    Posts:
    100
    I have tried this and I'm a little closer to final. The problem is I need box collider not sphere collider.
    Code (CSharp):
    1. using UnityEngine;
    2. using System;
    3. using System.Collections.Generic;
    4.  
    5. public class SuperCharacterController : MonoBehaviour {
    6.    
    7.     [SerializeField]
    8.     float radius = 0.5f;
    9.    
    10.     private bool contact;
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.        
    15.         contact = false;
    16.        
    17.         foreach (Collider col in Physics.OverlapSphere(transform.position, radius))
    18.         {
    19.             Vector3 contactPoint = col.ClosestPointOnBounds(transform.position);
    20.            
    21.             Vector3 v = transform.position - contactPoint;
    22.            
    23.             transform.position += Vector3.ClampMagnitude(v, Mathf.Clamp(radius - v.magnitude, 0, radius));
    24.            
    25.             contact = true;
    26.         }
    27.     }
    28.  
    29. }