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

Trying to make the player move but blocked by a "blocking"

Discussion in 'Scripting' started by Midasftw, Aug 14, 2022.

  1. Midasftw

    Midasftw

    Joined:
    Aug 14, 2022
    Posts:
    4
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     private BoxCollider2D boxCollider;
    8.     private Vector3 moveDelta;
    9.     private RaycastHit2D hit;
    10.  
    11.     private void Start()
    12.     {
    13.         boxCollider = GetComponent<BoxCollider2D>();
    14.     }
    15.  
    16.     private void FixedUpdate()
    17.     {
    18.         float x = Input.GetAxisRaw("Horizontal");
    19.         float y = Input.GetAxisRaw("Vertical");
    20.      
    21.                 // Reset moveDelta
    22.         moveDelta = new Vector3(x,y,0);
    23.      
    24.         //Swap Sprite direction, whether your going right or left
    25.         if(moveDelta.x > 0)
    26.             transform.localScale = Vector3.one;
    27.         else if (moveDelta.x < 0)
    28.             transform.localScale = new Vector3(-1, 1, 1);
    29.      
    30.         // Make sure we can move in this direction, by casting a box there first, if the box returns null, we're free to move.
    31.         hit = Physics2D.BoxCast(transform.position,boxCollider.size,0, new Vector2(0,moveDelta.y), Mathf.Abs(moveDelta.y * Time.deltaTime), LayerMask.GetMask("Actor","Blocking"));
    32.         if (hit.collider == null)
    33.         {
    34.             // Make this thing move!
    35.             transform.Translate(0, moveDelta.y * Time.deltaTime, 0);
    36.         }
    37.      
    38.         hit = Physics2D.BoxCast(transform.position,boxCollider.size, 0, new Vector2(moveDelta.x,0), Mathf.Abs(moveDelta.x * Time.deltaTime), LayerMask.GetMask("Actor","Blocking"));
    39.         if (hit.collider == null)
    40.         {
    41.             // Make this thing move!
    42.         transform.Translate(moveDelta.x * Time.deltaTime, 0, 0);
    43.         }
    44.     }

    I want the "Actor" to move and be blocked by "Blocking"
    I tried to consult the tutorial and read the comments to no avail
    I expected the "Actor" to be blocked by "Blocking"
    The "Actor" moves over the "Blocking"
    I used this video, it is on the right time
     
    Last edited: Aug 14, 2022
  2. Midasftw

    Midasftw

    Joined:
    Aug 14, 2022
    Posts:
    4
    im really new to unity and making games in general so help would be appricated
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Please edit your post to use code-tags and try to describe what you want beyond a rough, hard to understand thread title please.

    If you're new and this is copied code from a tutorial then it's best to consult the tutorial again. If you don't understand the code then it's going to be difficult to explain how to extend it. If you do understand it then please explain how you want to extend it in more detail.

    Thanks.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,966
    Yes, as Mel suggests above, we cannot read your mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    You may edit your post above.
     
  5. Midasftw

    Midasftw

    Joined:
    Aug 14, 2022
    Posts:
    4
    sorry! should work now
     
    Last edited: Aug 14, 2022
  6. Midasftw

    Midasftw

    Joined:
    Aug 14, 2022
    Posts:
    4
    Hey could you try and solve my problem now?
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    If the behaviour is in the 7+ hour tutorial then can you not simply follow the tutorial?
     
    Kurt-Dekker likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,966
    With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

    Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

    https://forum.unity.com/threads/col...-unity-physic-rigidbody.1216875/#post-7763061

    https://forum.unity.com/threads/oncollisionenter2d-not-being-called.1266563/#post-8044121
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,966
    Perhaps you didn't do the tutorial correctly? Be sure you didn't skip either Step 1 or Step 2 below:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!