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

Question Having trouble freezing an object in place.

Discussion in 'Scripting' started by LucioHernandez, Apr 23, 2022.

  1. LucioHernandez

    LucioHernandez

    Joined:
    Apr 21, 2022
    Posts:
    10
    Hello, I'm trying to make this game where you can move and rotate a platform around, and then use it to cross gaps and obstacles, but the problem is, the players can boost themselves up into infinity with this, so I want to be able to check when the player is touching the platform, and freeze it in place, and once they are off the platform, be able to move it again, but I've looked in so many places and I can't find the right solution, because my platform is using a BoxCollider2D and not a RigidBody2D, help!
    Code (CSharp):
    1. public class RotateWithKeyboard : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField] private float speed = .1f;
    5.     Vector2 lastClickedPos;
    6.     public float followSpeed = 10f;
    7.     bool moving;
    8.  
    9.     private BoxCollider2D coll;
    10.     private void Start()
    11.     {
    12.         coll = GetComponent<BoxCollider2D>();
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.             if (Input.GetKey(KeyCode.Q))
    18.             {
    19.                 transform.Rotate(0, 0, -360 * speed * Time.deltaTime);
    20.             }
    21.             else if (Input.GetKey(KeyCode.E))
    22.                 transform.Rotate(0, 0, 360 * speed * Time.deltaTime);
    23.  
    24.             if (Input.GetMouseButtonDown(0))
    25.             {
    26.                 lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    27.                 moving = true;
    28.             }
    29.             if (moving && (Vector2)transform.position != lastClickedPos)
    30.             {
    31.                 float step = followSpeed * Time.deltaTime;
    32.                 transform.position = Vector2.MoveTowards(transform.position, lastClickedPos, step);
    33.             }
    34.             else
    35.             {
    36.                 moving = false;
    37.             }
    38.         }
    39.     private void OnCollisionEnter2D(Collision2D collision)
    40.     {
    41.         if (collision.gameObject.CompareTag("Player"))
    42.         {
    43.             Freeze();
    44.         }
    45.     }
    46.     private void Freeze()
    47.     {
    48.         // Freezes Rotation and Movement
    49.     }
    50.  
    51.     private void OnCollisionExit2D(Collision2D collision)
    52.     {
    53.         if (collision.gameObject.CompareTag("Player"))
    54.         {
    55.             Unfreeze();
    56.         }
    57.     }
    58.     private void Unfreeze()
    59.     {
    60.         // Unfreezes Rotation and Movement
    61.     }
    62. }
     
  2. LucioHernandez

    LucioHernandez

    Joined:
    Apr 21, 2022
    Posts:
    10
  3. arfish

    arfish

    Joined:
    Jan 28, 2017
    Posts:
    777
    Hello,

    What effect do you expect the comment in the method Freeze() should result in?

    Code (CSharp):
    1. private void Freeze()
    2.     {
    3.         // Freezes Rotation and Movement
    4.     }