Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

My collider stops working when I user EulerAngles! Please help!

Discussion in 'Physics' started by darkesco, Aug 29, 2019.

  1. darkesco

    darkesco

    Joined:
    Mar 19, 2015
    Posts:
    27
    Hi. I'm making a game where the player (camera with a box collider) falls down a well. The analog thumb stick on the controller moves the player in the X and Z axis as they fall in the Y axis down a well. Everything worked fine until I wanted to have the camera spin on the Y axis to give more feeling to the game. So I spun the camera. Unfortunately, the controls were screwed up from the players perspective. For example if the camera is rotated 90 degrees, and the play pressed up, the view moved sideways not "up." (from the player's perspective). If the camera is upside down due to rotation, then pressing up moves the camera "down" (from the player's perspective). That made sense to me since the camera is technically moving in the correct direction, but not what I needed. So I multiplied the rotation by the position and it worked great. No matter what rotation was applied to the camera, pressing up moved the camera up from the player's perspective...But, the camera's box collider that was working perfectly fine, just started going through the walls. I can have the controls I want or I can have a collider, but not both. Here is the code that works with the controls but not the collider: Pos.x and Pos.z is calculated before this code by user thumb stick input. Pos.y is simulated (not physics) gravity of the well.

    Code (CSharp):
    1.  
    2. Vector3 input = Quaternion.Euler(0, transform.eulerAngles.y, 0) * Pos;
    3.  //transform.Rotate(0, Y, 0);
    4.  transform.position = input;
    5.  
    input is a multiplication of the Euler rotation and Pos position, This breaks the collider.

    On the other-hand:
    If I just use transform.position = Pos (just position, no rotation) then the collider works, but of course my controls are screwed up.

    I experimented with rigidbodies, character controllers, and a myriad of techniques and I'm at a loss. Please help! This game is VR compatible and I'm afraid that none of my friends will puke if I can't add a spin to falling down a well.
     
  2. darkesco

    darkesco

    Joined:
    Mar 19, 2015
    Posts:
    27
    Upon further investigations, the collisions never worked. The well is cube shaped, and I had scripted the boundaries of the X and Z ranges for the camera. After using a rigidbody, I can somewhat control the camera object, but it's completely screwed.

    Code (CSharp):
    1. float moveHorizontal = Input.GetAxis("Horizontal");
    2.         float moveVertical = Input.GetAxis("Vertical");
    3.  
    4.         Vector3 movement = new Vector3(moveHorizontal * 50, 0, moveVertical * 50);
    5.  
    6.         RB.AddForce(movement * MoveSpeed);
    7.         RB.AddForce(Vector3.down * GameController.control.FallSpeed);
    Instead of just stopping the camera object from passing through the walls, the walls can now be used to slow the fall down. The player can literately stop the camera just by rubbing it on the well walls. It looks like I'll have to try another route. I just need the camera to stay inside a simple mesh and not use real physics.I wish there was a way to just use colliders as a boundary and not a physics simulation.
     
  3. darkesco

    darkesco

    Joined:
    Mar 19, 2015
    Posts:
    27
    Changed physics material to all 0's. It's kind of working now. This really seems like overkill just to stay in some boundaries, but whatever.
     
  4. darkesco

    darkesco

    Joined:
    Mar 19, 2015
    Posts:
    27
    The "Bug" is my lack of knowledge. At this point I can either fake gravity on the Z-axis with a character controller, or learn how to apply directional movement after adding torque. This is simply a beginners question that I am unable to ask properly and most likely will result in me having to learn some calculus.
     
  5. segant

    segant

    Joined:
    May 3, 2017
    Posts:
    101
    You can use Mathf.Sin and Mathf.cos then use for camera transform.lookat . It's the easy way for you. I dont understand what you want but if you know math you can do everything in this engine. If you want to rotate a vector depending on vector axis. Then look at Quarnetion.angleaxis For camera wall detection you can use trigger or physics.raycast. And input will override collision force because of Fixed Update. In every cycle you have to check if camera collided before setting its position. So you need a bool for that and OntriggerEnter or stay function.If it's colliding then you have to push it according to collision normal or reverse vector of two objects direction vector such as
    Code (CSharp):
    1. (collider.transform.position-camera.main.transform.position).normalized*Force
    if you set force negative then direction will be to into the camera. For positive vica-versa.
     
    Last edited: Sep 2, 2019
  6. darkesco

    darkesco

    Joined:
    Mar 19, 2015
    Posts:
    27
    Thanks for the reply. I managed to get it! It's not perfect, but it does what I need it to. No calculus needed. Just had to grab Rigidbody.rotation

    Code (CSharp):
    1.  
    2.     private void FixedUpdate()
    3.     {
    4.         float moveHorizontal = Input.GetAxis("Horizontal");
    5.         float moveVertical = Input.GetAxis("Vertical");
    6.         Vector3 MoveIt = new Vector3(moveHorizontal * 20, -10 + GameController.control.Gravity, moveVertical * 20);
    7.  
    8.         if (Rot == true)
    9.         {
    10.             RB.AddTorque(0, 0.1f, 0);
    11.             Rot = false;
    12.         }
    13.  
    14.         RB.AddForce(RB.rotation* MoveIt, ForceMode.Force);
    15.  
    16.     }
    17.