Search Unity

unity object is vibrating on collission

Discussion in '2D' started by kristiandgrey, Feb 20, 2021.

  1. kristiandgrey

    kristiandgrey

    Joined:
    Nov 19, 2019
    Posts:
    6
    hi!

    im having a problem with my collission. my bullet is colliding with other objects fine, but that is because I've locked its z rotation.


    on that link there it shows the problem as it is hard for me to explain it. both rigidBodys are continuous collision detection and here are my move and rotate scripts for my player:

    Code (CSharp):
    1. public class move : MonoBehaviour
    2. {
    3.     [Space]
    4.     public float MoveSpeed, horizontal, vertical;
    5.     public KeyCode up, down, left, right;
    6.     [Space]
    7.     [Space]
    8.     private static Vector3 mouse_pos;
    9.     private static Vector3 obj_pos;
    10.     public float angle;
    11.     private static Rigidbody2D body;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         body = gameObject.GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.      
    22.  
    23.     }
    24.     public GameObject FindCameraUNI(GameObject g)
    25.     {
    26.         GameObject root = g.transform.root.gameObject;
    27.         foreach (Transform t in root.transform)
    28.         {
    29.             if (t.tag == "PlayerCamera")
    30.             {
    31.                 return t.gameObject;
    32.             }
    33.         }
    34.         print("couldnt find camera");
    35.         return g;
    36.     }
    37.     private void FixedUpdate()
    38.     {
    39.         body.interpolation = RigidbodyInterpolation2D.Interpolate;
    40.         horizontal = Input.GetAxisRaw("Horizontal");
    41.         vertical = Input.GetAxisRaw("Vertical");
    42.         mouse_pos = Input.mousePosition;
    43.         mouse_pos.z = 5.23f;
    44.         obj_pos = FindCameraUNI(gameObject).GetComponent<Camera>().WorldToScreenPoint(gameObject.transform.position);
    45.         mouse_pos.x = mouse_pos.x - obj_pos.x;
    46.         mouse_pos.y = mouse_pos.y - obj_pos.y;
    47.         angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
    48.         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    49.         body.velocity = new Vector2(horizontal * MoveSpeed, vertical * MoveSpeed);
    50.     }
    51. }
    52.  

    keep in mind my collider is on a child object.
    both rigidbodys are on sleeping move:NEVER SLEEP and hav eno gravity. im new to the unity collision system in this aspect and i am completely stumped. if any info is needed i am happy to provide!(DISCLAIMER the recording was shot on 60hz while i was editing on 240hz, so the vibration looks alot worse!!!!)
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    If you don't have rotation locked for the player it will do this. The enemy bullet is causing torque to be applied to the player, which your rotation code is trying to rotate as well and the two are fighting. You can lock the rotation in the player and still rotate it, as a matter of fact you can still use Rigidbody2D.rotation to rotate instead of using transform.rotation.
     
    kristiandgrey likes this.
  3. kristiandgrey

    kristiandgrey

    Joined:
    Nov 19, 2019
    Posts:
    6
    thanks so much man! cant thank you enough :)
     
  4. kristiandgrey

    kristiandgrey

    Joined:
    Nov 19, 2019
    Posts:
    6
    in the video, you might be able to see my character being knocked back by the bullet force, is there anyway to stop that force being applied to my gameobject while still usign collision colliders?
     
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    I'm not sure, I've always used triggers for bullets and apply my own force if I want it. You might be able to set velocity to zero in OnCollisionEnter2D. Maybe @MelvMay might be able to enlighten us both of how to do that.