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

help with my game please

Discussion in '2D' started by ManWStudio, Dec 15, 2019.

  1. ManWStudio

    ManWStudio

    Joined:
    Dec 15, 2019
    Posts:
    3
    Good, I am developing a project for the university and I am not very good at programming. I'm doing a top down mobile game and I need the character to move with buttons but I don't want to lose the movement in a grid, could you please help me? I'm desperate

    this code;
    https://pastebin.com/K6XdYyB9
     
  2. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    Please, post your code here, using code tags, thank you.
     
  3. APSchmidtOfOld

    APSchmidtOfOld

    Joined:
    Aug 8, 2016
    Posts:
    4,473
    There is no way your script will work if the code is translated...
     
  4. ManWStudio

    ManWStudio

    Joined:
    Dec 15, 2019
    Posts:
    3

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public enum Direction
    5. {
    6.     up, down, right, left
    7. }
    8. public class Movement : MonoBehaviour
    9. {
    10.     Animator anim;
    11.     Vector2 targetPosition;
    12.     Direction direction;
    13.     public float speed = 5f;
    14.     public LayerMask obstacle;
    15.     private void Start()
    16.     {
    17.         anim = GetComponent<Animator>();
    18.         targetPosition = transform.position;
    19.         direction = Direction.down;
    20.     }
    21.     void Update()
    22.     {
    23.         Vector2 axisDirection = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    24.         anim.SetInteger("Direccion", (int)direction);
    25.         if (axisDirection != Vector2.zero && targetPosition == (Vector2)transform.position)
    26.         {
    27.             if (Mathf.Abs(axisDirection.x) > Mathf.Abs(axisDirection.y))
    28.             {
    29.                 if (axisDirection.x > 0)
    30.                 {
    31.                     direction = Direction.right;
    32.                     if (!CheckCollision)
    33.                         targetPosition += Vector2.right;
    34.                 }
    35.                 else
    36.                 {
    37.                     direction = Direction.left;
    38.                     if (!CheckCollision)
    39.                         targetPosition -= Vector2.right;
    40.                 }
    41.             }
    42.             else
    43.             {
    44.                 if (axisDirection.y > 0)
    45.                 {
    46.                     direction = Direction.up;
    47.                     if (!CheckCollision)
    48.                         targetPosition += Vector2.up;
    49.                 }
    50.                 else
    51.                 {
    52.                     direction = Direction.down;
    53.                     if (!CheckCollision)
    54.                         targetPosition -= Vector2.up;
    55.                 }
    56.             }
    57.         }
    58.         transform.position = Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
    59.     }
    60.     bool CheckCollision
    61.     {
    62.         get
    63.         {
    64.             bool col = true;
    65.             RaycastHit2D rh;
    66.             Vector2 dir = Vector2.zero;
    67.             if (direction == Direction.down)
    68.                 dir = Vector2.down;
    69.             if (direction == Direction.up)
    70.                 dir = Vector2.up;
    71.             if (direction == Direction.left)
    72.                 dir = Vector2.left;
    73.             if (direction == Direction.right)
    74.                 dir = Vector2.right;
    75.             rh = Physics2D.Raycast(transform.position, dir, 1, obstacle);
    76.             return rh.collider != null;
    77.             return col;
    78.         }
    79.     }
    80. }
    sorry