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

I need help adding collision to my walking script

Discussion in '2D' started by ayuuDubbed, Aug 8, 2020.

  1. ayuuDubbed

    ayuuDubbed

    Joined:
    Aug 8, 2020
    Posts:
    4
    I'm currently trying to make an IOS app with unity, as you can see in the picture I have a character, boxes and two buttons

    I have made a script that lets my character move when the buttons are pressed,
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.EventSystems;
    5.  
    6. public class Movement : MonoBehaviour
    7. {
    8.     public Button storeButtonRight;
    9.     public Button storeButtonLeft;
    10.     public float speed = 100f;
    11.     public Transform player;
    12.  
    13.     void OnEnable()
    14.     {
    15.         // storeButton.onClick.AddListener(MyFunction);//adds a listener for when you click the button
    16.         storeButtonRight.onClick.AddListener(() => moveCharacter(new Vector2(1, 0f)));
    17.         storeButtonLeft.onClick.AddListener(() => moveCharacter(new Vector2(-1, 0f)));
    18.     }
    19.  
    20.     void moveCharacter(Vector2 direction)
    21.     {
    22.       player.Translate(direction * speed * Time.deltaTime);
    23.     }
    24. }
    25.  
    This works and all, but my main problem is that my Collision won't work, I added collision 2d objects to all the objects, but the player still walks right through them any help please?
     

    Attached Files:

  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
  3. ayuuDubbed

    ayuuDubbed

    Joined:
    Aug 8, 2020
    Posts:
    4