Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Sprite Wont stay where it was locked.

Discussion in 'Code Editors & IDEs' started by aldup, May 6, 2022.

  1. aldup

    aldup

    Joined:
    May 6, 2022
    Posts:
    2
    I'm making a 2D game based on the tv show Hole in the wall. I'm a very new programmer. I have figured out in theory how to make the sprite go "through the hole" by making a sprite of the same shape(A holder) have a box collider and "lock" the piece in place (disables all movement allowed by keys), however here comes my problem.
    The sprite will get the status "locked" when it enters the holder's box collider, but it won't stay in place and will fling in a direction, and fall (There is no gravity on it), so I'm wondering if there is a way I can get the sprite to line with the holder and stay there?
    If you have any ideas please reply, I'm using Visual Studios in c#
    Edit: I also made it to where you can swap to different sprites, this is still a WIP game.

    Here's my code:

    public float topSpeed;
    public float runSpeed;
    public string status = " ";

    public Sprite[] spriteArray;
    public SpriteRenderer spriteRenderer;
    public Sprite newSprite;
    void ChangeSprite(Sprite newSprite)
    {
    spriteRenderer.sprite = newSprite;
    }

    Rigidbody2D rB;
    SpriteRenderer sR;
    Transform playerPosition;

    // Start is called before the first frame update
    void Start()
    {
    rB = gameObject.GetComponent<Rigidbody2D>();
    playerPosition = gameObject.transform;
    sR = gameObject.GetComponent<SpriteRenderer>();
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.gameObject.tag == "Holder")
    {
    transform.position = collision.gameObject.transform.position;
    status = "locked";
    }
    }

    // Update is called once per frame
    void Update()
    {

    if ((Input.GetKey(KeyCode.A) && status != "locked") && Mathf.Abs(rB.velocity.x) < topSpeed)
    {
    rB.AddForce(new Vector2(-runSpeed, 0));

    }
    else if ((Input.GetKey(KeyCode.D) && Mathf.Abs(rB.velocity.x) < topSpeed) && status != "locked")
    {
    rB.AddForce(new Vector2(runSpeed, 0));
    }
    else if ((Input.GetKey(KeyCode.W) && Mathf.Abs(rB.velocity.x) < topSpeed) && status != "locked")
    {
    rB.AddForce(new Vector2(0, runSpeed));
    }
    else if ((Input.GetKey(KeyCode.S) && Mathf.Abs(rB.velocity.x) < topSpeed) && status != "locked")
    {
    rB.AddForce(new Vector2(0, -runSpeed));
    }

    if (Input.GetKeyDown(KeyCode.F))
    {
    ChangeSprite(newSprite);
    spriteRenderer.sprite = spriteArray[4];
    }
    if (Input.GetKeyDown(KeyCode.E))
    {
    ChangeSprite(newSprite);
    spriteRenderer.sprite = spriteArray[3];
    }
    if (Input.GetKeyDown(KeyCode.Q))
    {
    ChangeSprite(newSprite);
    spriteRenderer.sprite = spriteArray[2];
    }
    if (Input.GetKeyDown(KeyCode.R))
    {
    ChangeSprite(newSprite);
    spriteRenderer.sprite = spriteArray[1];
    }
    if (Input.GetKeyDown(KeyCode.Space))
    {
    ChangeSprite(newSprite);
    spriteRenderer.sprite = spriteArray[0];
    }
    }

    }
     

    Attached Files:

  2. aldup

    aldup

    Joined:
    May 6, 2022
    Posts:
    2
    Resolved, made it on trigger enter and made velocity 0 when in lock