Home Game Development c# – How one can make capturing rotation restricted in Unity?

c# – How one can make capturing rotation restricted in Unity?

0
c# – How one can make capturing rotation restricted in Unity?

[ad_1]

enter image description hereI am simply beginning to make video games, and I’ve an issue that my weapon rotates 360 levels, I would like there to be a rotation restrict. Additionally, my participant can transfer within the different route, and the weapon additionally saves its coordinates, how can I repair this?

void Replace()
{
    Vector3 distinction = Digicam.most important.ScreenToWorldPoint(Enter.mousePosition) - remodel.place;
    float rotateZ = Mathf.Atan2(distinction.y, distinction.x) * Mathf.Rad2Deg;
    remodel.rotation = Quaternion.Euler(0f, 0f, rotateZ);
}

participant motion code

public class CharacterController2D : MonoBehaviour
{
    [SerializeField] non-public float m_JumpForce = 400f; // Quantity of drive added when the participant jumps.
    [Range(0, 1)] [SerializeField] non-public float m_CrouchSpeed = .36f; // Quantity of maxSpeed utilized to crouching motion. 1 = 100%
    [Range(0, .3f)] [SerializeField] non-public float m_MovementSmoothing = .05f;  // How a lot to clean out the motion
    [SerializeField] non-public bool m_AirControl = false;                         // Whether or not or not a participant can steer whereas leaping;
    [SerializeField] non-public LayerMask m_WhatIsGround;                          // A masks figuring out what's floor to the character
    [SerializeField] non-public Rework m_GroundCheck;                           // A place marking the place to verify if the participant is grounded.
    [SerializeField] non-public Rework m_CeilingCheck;                          // A place marking the place to verify for ceilings
    [SerializeField] non-public Collider2D m_CrouchDisableCollider;                // A collider that will likely be disabled when crouching
    
    const float k_GroundedRadius = .2f; // Radius of the overlap circle to find out if grounded
    non-public bool m_Grounded;            // Whether or not or not the participant is grounded.
    const float k_CeilingRadius = .2f; // Radius of the overlap circle to find out if the participant can get up
    non-public Rigidbody2D m_Rigidbody2D;
    non-public bool m_FacingRight = true;  // For figuring out which means the participant is presently dealing with.
    non-public Vector3 m_Velocity = Vector3.zero;

    [Header("Events")]
    [Space]

    public UnityEvent OnLandEvent;

    [System.Serializable]
    public class BoolEvent : UnityEvent<bool> { }

    public BoolEvent OnCrouchEvent;
    non-public bool m_wasCrouching = false;

    non-public void Awake()
    {
        m_Rigidbody2D = GetComponent<Rigidbody2D>();

        if (OnLandEvent == null)
            OnLandEvent = new UnityEvent();

        if (OnCrouchEvent == null)
            OnCrouchEvent = new BoolEvent();
    }

    non-public void FixedUpdate()
    {
        bool wasGrounded = m_Grounded;
        m_Grounded = false;

        // The participant is grounded if a circlecast to the groundcheck place hits something designated as floor
        // This may be performed utilizing layers as a substitute however Pattern Property won't overwrite your mission settings.
        Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.place, k_GroundedRadius, m_WhatIsGround);
        for (int i = 0; i < colliders.Size; i++)
        {
            if (colliders[i].gameObject != gameObject)
            {
                m_Grounded = true;
                if (!wasGrounded)
                    OnLandEvent.Invoke();
            }
        }
    }


    public void Transfer(float transfer, bool crouch, bool bounce)
    {
        // If crouching, verify to see if the character can get up
        if (crouch)
        {
            // If the character has a ceiling stopping them from standing up, maintain them crouching
            if (Physics2D.OverlapCircle(m_CeilingCheck.place, k_CeilingRadius, m_WhatIsGround))
            {
                crouch = true;
            }
        }

        //solely management the participant if grounded or airControl is turned on
        if (m_Grounded || m_AirControl)
        {

            // If crouching
            if (crouch)
            {
                if (!m_wasCrouching)
                {
                    m_wasCrouching = true;
                    OnCrouchEvent.Invoke(true);
                }

                // Scale back the velocity by the crouchSpeed multiplier
                transfer *= m_CrouchSpeed;

                // Disable one of many colliders when crouching
                if (m_CrouchDisableCollider != null)
                    m_CrouchDisableCollider.enabled = false;
            } else
            {
                // Allow the collider when not crouching
                if (m_CrouchDisableCollider != null)
                    m_CrouchDisableCollider.enabled = true;

                if (m_wasCrouching)
                {
                    m_wasCrouching = false;
                    OnCrouchEvent.Invoke(false);
                }
            }

            // Transfer the character by discovering the goal velocity
            Vector3 targetVelocity = new Vector2(transfer * 10f, m_Rigidbody2D.velocity.y);
            // After which smoothing it out and making use of it to the character
            m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);

            // If the enter is transferring the participant proper and the participant is dealing with left...
            if (transfer > 0 && !m_FacingRight)
            {
                // ... flip the participant.
                Flip();
            }
            // In any other case if the enter is transferring the participant left and the participant is dealing with proper...
            else if (transfer < 0 && m_FacingRight)
            {
                // ... flip the participant.
                Flip();
            }
        }
        // If the participant ought to bounce...
        if (m_Grounded && bounce)
        {
            // Add a vertical drive to the participant.
            m_Grounded = true;
            m_Rigidbody2D.AddForce(new Vector2(0f, m_JumpForce));
        }
    }


    non-public void Flip()
    {
        // Swap the best way the participant is labelled as dealing with.
        m_FacingRight = !m_FacingRight;

        // Multiply the participant's x native scale by -1.
        Vector3 theScale = remodel.localScale;
        theScale.x *= -1;
        remodel.localScale = theScale;
        //remodel.Rotate (0f, 180f, 0f);
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here