Search Unity

Collision issue

Discussion in 'Physics' started by crazygeekman, Feb 24, 2019.

  1. crazygeekman

    crazygeekman

    Joined:
    Feb 24, 2019
    Posts:
    2
    Hello everyone !

    I'm a beginner in Unity so please be indulgent ;)

    I would like to move a cube with the following keys : Z, Q, S, D
    Z for up, S for down, Q for left and D for right on x and z axis.
    And E for up and F for down on y axe.

    My problem is the cube doesn't take into account the collisions with other objects and pierce them.
    I attached to this cube RigidBody and BoxCollider components.

    Here is the code i use for the movement :

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraController : MonoBehaviour
    4. {
    5.     public float speed = 20f;
    6.     // Cube position
    7.     Vector3 pos;
    8.     // Cube direction
    9.     Vector3 view;
    10.     // Way to go
    11.     Vector3 way;
    12.  
    13.     private void Start()
    14.     {
    15.         pos = transform.position;
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.  
    21.         view = transform.forward;
    22.         way = view * speed * Time.deltaTime;
    23.  
    24.         if (Input.GetKey(KeyCode.Z))
    25.             pos += new Vector3(way.x, 0, way.z);
    26.  
    27.         if (Input.GetKey(KeyCode.S))
    28.             pos += new Vector3(-way.x, 0, -way.z);
    29.  
    30.         if (Input.GetKey(KeyCode.Q))
    31.         {
    32.             Vector2 temp = Vector2.Perpendicular(new Vector2(view.x, view.z));
    33.             view = new Vector3(temp.x, 0, temp.y);
    34.             way = view * speed * Time.deltaTime;
    35.             pos += new Vector3(way.x, 0, way.z);
    36.         }
    37.  
    38.         if (Input.GetKey(KeyCode.D))
    39.         {
    40.             Vector2 temp = Vector2.Perpendicular(new Vector2(view.x, view.z));
    41.             view = new Vector3(temp.x, 0, temp.y);
    42.             way = view * speed * Time.deltaTime;
    43.             pos += new Vector3(-way.x, 0, -way.z);
    44.         }
    45.  
    46.         if (Input.GetKey(KeyCode.E))
    47.             pos.y += speed * Time.deltaTime;
    48.  
    49.         if (Input.GetKey(KeyCode.F))
    50.             pos.y -= speed * Time.deltaTime;
    51.  
    52.         transform.position = pos;
    53.     }
    54. }
    What I should to do in order to have the desired result ?

    Thank you :)
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,512
    Hi, and welcome to Unity forums!

    You need to use a different approach. The object must be moved using Rigidbody.AddForce (not Transform) so the physics could handle the collisions. You may apply a velocity increment to the object with Rigidbody.AddForce(velocity, ForceMode.VelocityChange).

    I'd do something like this:
    1. Figure out a normalized direction vector of the movement based on the keys. For example, if Z "up" is pressed, then the direction vector would be Vector3(0, 1, 0). If Z "up" and Q "left" are pressed, then the direction would be Vector3(-1, 1, 0).normalized. If no key is pressed then the direction vector would be 0,0,0.
    2. Compute the velocity the rigidbody should have. This would be simply: desiredVelocity = direction * speed. Note that we're specifying the velocity directly instead of the distance to move, so no Time.deltaTime is used.
    3. Apply the desired velocity taking into account the current velocity the rigidbody is moving at. The velocity difference can be computed by subtracting the current velocity from the desired velocity, so it would be like this:

      myRigidbody.AddForce (desiredVelocity - myRigidbody.velocity, ForceMode.VelocityChange);
    Important: put all this code in FixedUpdate, not in Update. FixedUpdate is called before each physics step, while Update is called at the screen's refresh rate.

    Have fun!
     
  3. crazygeekman

    crazygeekman

    Joined:
    Feb 24, 2019
    Posts:
    2
    Thank you very much ! :)
    I succeded to fix my issue thanks to your help :D