Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Freeze Z rotation not working

Discussion in '2D' started by BladeXD, Jan 25, 2021.

Thread Status:
Not open for further replies.
  1. BladeXD

    BladeXD

    Joined:
    Jan 11, 2021
    Posts:
    3
    Hey, I'm trying to make a top-down shooter but for some reason my sprite keeps rotating even though I have Z rotation frozen in RigidBody2D

    I've read that manually coding Inertia could fix the problem as it has 2 child game objects but how do I do this?

    Sorry if this is in the wrong forum.
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,345
    Can you show some code related to the sprite's movement? Like anything controlling that Rigidbody2D or the sprite.
     
    BladeXD likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    So that would suggest that the Rigidbody2D is not controlling the Transform and that one of your scripts is doing that i.e. modifying the Transform rotation. The Rigidbody2D doesn't have any power over you bypassing its goal of updating the Transform and you doing it. A quick check is to set the Rigidbody2D body-type to Static and see if it still rotates. This would mean you're modifying the Transform.

    Not sure what or where you read but there's no need for a fix for a problem that doesn't exist. Rigidbody2D is a Box2D body which has a hardcoded ability to restrict any rotation by enforcing infinite rotational inertia (it's internally set to zero) so you setting it won't make any difference.

    So ensure you're not modify the Transform but if you think not then as was said above, please post your movement code.
     
    makaka-org and BladeXD like this.
  4. BladeXD

    BladeXD

    Joined:
    Jan 11, 2021
    Posts:
    3
    Hey guys thanks for the replies, only just got chance to login and check. The problem is if I set the RigidBody to static the gun and firepoint don't rotate towards my mouse cursor. This is the movement code I'm using and I want it so the weapon and firepoint rotate (which are both currently child objects of my player) without my player sprite rotating with them.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float moveSpeed = 5f;
    8.     public Rigidbody2D rb;
    9.     public Camera cam;
    10.     Vector2 movement;
    11.     Vector2 mousePos;
    12.  
    13.     private void Update()
    14.  
    15.  
    16.     {
    17.         movement.x = Input.GetAxisRaw("Horizontal");
    18.         movement.y = Input.GetAxisRaw("Vertical");
    19.  
    20.         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    21.  
    22.  
    23.  
    24.  
    25.     }
    26.  
    27.  
    28.     private void FixedUpdate()
    29.     {
    30.         rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    31.  
    32.         Vector2 lookDir = mousePos - rb.position;
    33.         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
    34.         rb.rotation = angle;
    35.  
    36.  
    37.  
    38.     }
    39.  
    40. }




    Code (CSharp):
    1. Shooting code:
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public class Shooting : MonoBehaviour
    8. {
    9.  
    10.     public Transform firePoint;
    11.     public GameObject bulletPrefab;
    12.  
    13.     public float bulletForce = 20f;
    14.     private float timeWhenAllowedNextShoot = 0f;
    15.     public float timeBetweenShooting = 1f;
    16.     private void Update()
    17.     {
    18.  
    19.      
    20.         {
    21.             if (timeWhenAllowedNextShoot <= Time.time)
    22.             {
    23.                 if (Input.GetButton("Fire1"))
    24.                 {
    25.                     Shoot();
    26.                     timeWhenAllowedNextShoot = Time.time + timeBetweenShooting;
    27.                 }
    28.             }
    29.      
    30.          
    31.         }
    32.         void Shoot()
    33.         {
    34.             GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
    35.             Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
    36.             rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    37.  
    38.  
    39.  
    40.             bullet.gameObject.transform.eulerAngles = new Vector3(0, 0, 90);
    41.  
    42.         }
    43.  
    44.     }
    45. }
     
    Last edited: Jan 27, 2021
    Cornysam likes this.
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,345
    Can you edit this message and use code tags?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    You're changing the rotation here. Freeze rotation doesn't mean it'll ignore you when you change the rotation directly. It'll stop forces from doing it.

    Also note that this would still change the rotation of a Static body so no idea what you mean about it not then rotating.
     
  7. BladeXD

    BladeXD

    Joined:
    Jan 11, 2021
    Posts:
    3
    When I put the player RigidBody2D to static it doesn't rotate but the bullet will only go straight and not towards my cursor.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    This was just an observation. What about the result from your question which is related to it rotating even though you set a rotation constraint? As I said above, you're explicitly setting the RB rotation.

    Cannot help you if you don't provide feedback on suggestions.
     
Thread Status:
Not open for further replies.