Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Object goes through the plane

Discussion in 'Physics' started by Arsandis, Oct 31, 2018.

  1. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    17
    Hey,
    I have written a script to pick an object with a rigidbody component by the ray casted from the controller and move it around.
    I have added this script to pick it up and drop it and have attached it to the objects I want to pick.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PickUp : PropBase
    5. {
    6.     private Rigidbody rb;
    7.     void Start()
    8.     {
    9.         rb = GetComponent<Rigidbody>();
    10.     }
    11.     public virtual void Store(Transform NewParent)
    12.     {
    13.         //The following stops the object being effected by physics while it's in the players hand
    14.         rb.isKinematic = true;
    15.         //And fixes it to the new parent it is given by the player script to follow.
    16.         transform.parent = NewParent;
    17.         //It then resets it's position and rotation to match it's new parent object
    18.         //transform.localRotation = Quaternion.identity;
    19.         //transform.localPosition = Vector3.zero;
    20.     }
    21.     public virtual void Release(Vector3 ThrowDir, float ThrowForce)
    22.     {
    23.         //On Release the object is made to be effected by physics again.
    24.         rb.isKinematic = false;
    25.         //Free itself from following it's parent object
    26.         transform.parent = null;
    27.         //And applies a burst of force for one frame to propel itself away from the player.
    28.         rb.AddForce(ThrowDir * ThrowForce, ForceMode.Impulse);
    29.     }
    30. }
    Problem is that when the object is attached to the controller, it passes through the plane or the floor and does not interact with it any more. It still collides with other objects but not with the plane.
    Will be helpful, if anyone can help me fix this.
     
  2. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    559
    well you can not make it kinematic and use forces to make it hover at/to some point in 3D space, a mini PID controller could come in handy, but it's getting complex haha
     
  3. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    17
    Yeah lol. Can you like, guide or tell me how to do it?
     
  4. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    559
    calculate the direction from the object to the point infront of you you want to hold it at and add force on that direction to the object
     
  5. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    17
    how do i add force on that direction to the object?
     
  6. SparrowsNest

    SparrowsNest

    Joined:
    Apr 6, 2017
    Posts:
    559
    something like that
    Code (CSharp):
    1. public class ForceExample{
    2.  
    3. Vector3 position// the position in world space where you want it to go, probably something like camera.transform.position+camera.transform.forward.
    4. Rigidbody rb; //the object you want to pickup
    5. float force; //you can use the distance with inverse squares and all those guys
    6.  
    7. void FixedUpdate(){
    8.  
    9. Vector3 direction = rb.position - position; //(it maybe the other way around, kinda high right now lol)
    10.  
    11. rb.addForce(direction.normalized * force, forceMode.impulse);
    12. }
    13. }
     
  7. Arsandis

    Arsandis

    Joined:
    Sep 25, 2018
    Posts:
    17
    Thanks a lot man. I'll give this a go.