21.5 C
New York
Monday, April 28, 2025

Unit: Easy get together/participant motion for a job -up play


So, to maintain it quick, I’m creating a job -to -down position -to -bottom sport. The participant (and the get together) can solely Transfer in 4 instructions. It appears actually easy, however I’ve not been capable of finding a superb reply on methods to set up the motion of the get together and I feel I’ve made it too sophisticated.

Right here is the present code for the participant:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;

    public Rigidbody2D rb;
    public Animator animator;

    public Vector2 motion;

    // Replace is named as soon as per body
    void Replace()
    {
        // Enter
        motion.x = Enter.GetAxisRaw("Horizontal");
        motion.y = Enter.GetAxisRaw("Vertical");

        animator.SetFloat("Horizontal", motion.x);
        animator.SetFloat("Vertical", motion.y);
        animator.SetFloat("Pace", motion.sqrMagnitude);
        
        if(Enter.GetAxisRaw("Horizontal") == 1 || Enter.GetAxisRaw("Horizontal") == -1 || Enter.GetAxisRaw("Vertical") == -1 || Enter.GetAxisRaw("Vertical") == 1)
        {
            animator.SetFloat("LastMovementX", Enter.GetAxisRaw("Horizontal"));
            animator.SetFloat("LastMovementY", Enter.GetAxisRaw("Vertical"));
        }
        
        if (Mathf.Abs(motion.x) > Mathf.Abs(motion.y))
        {
            motion.y = 0;
        }
        else
        {
            motion.x = 0;
        }
    }

    void FixedUpdate()
    {
        //Motion
        rb.MovePosition(rb.place + motion * moveSpeed * Time.fixedDeltaTime);
    }
}

This was a tutorial on-line and I believed it was nice! It really works effectively. The one drawback I’ve is when I’m inflicting the participant to stroll in a single path and press a unique arrow key, do they level in that path? I believed the (Mathf.abs (motion.x)> Mathf.abs (motion.y)) I might additionally keep away from that, however I suppose I used to be improper. Nor have I discovered a direct reply about this. I’ve tried this methodology:

if (Mathf.Abs(motion.x) > 0.01f)
    {

        motion.y = 0; // Disable vertical enter if a horizontal key's pressed

    }
    
    if (Mathf.Abs(motion.y) > 0.01f)
    {
        motion.x = 0;
    }

However this didn’t assist. He did not appear to vary something. It’s presupposed to deny the participant’s character to detect one other arrow key motion.

Now, in the issue of the get together. Initially, I believed I may copy all the things from the participant’s character and simply make sure that to vary issues, however that gave the impression to be harder than I believed. That is the code for my get together sprite:

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

public class FollowPlayer : MonoBehaviour
{
    
    public GameObject chief; // the sport object to comply with - assign in inspector
    public int steps; // variety of steps to remain behind - assign in inspector
    public int distance; //consistant distance to keep away from participant - assign in inspector
    public float moveSpeed = 5f;

    public Rigidbody2D rb;
    public Animator animator;

    non-public Queue report = new Queue();
    non-public Vector3 lastRecord;

    non-public Vector2 motion;

    void Replace() {

        motion.x = Enter.GetAxisRaw("Horizontal");
        motion.y = Enter.GetAxisRaw("Vertical");

        //set animations
        animator.SetFloat("Horizontal", motion.x);
        animator.SetFloat("Vertical", motion.y);
        animator.SetFloat("Pace", motion.sqrMagnitude);

        if(Enter.GetAxisRaw("Horizontal") == 1 || Enter.GetAxisRaw("Horizontal") == -1 || Enter.GetAxisRaw("Vertical") == -1 || Enter.GetAxisRaw("Vertical") == 1)
        {
            animator.SetFloat("LastMovementX", motion.x);
            animator.SetFloat("LastMovementY", motion.y);
        }
        
        if (Mathf.Abs(motion.x) > Mathf.Abs(motion.y))
        {
            motion.y = 0;
        }
        else
        {
            motion.x = 0;
        }
    }

    void FixedUpdate() {
        // report place of chief
        report.Enqueue(chief.remodel.place);

        //Motion
        rb.MovePosition(rb.place + motion * moveSpeed * Time.fixedDeltaTime);

        // take away final place from the report and use it for our personal
        if (report.Rely > steps) {
            this.remodel.place = report.Dequeue();
        }

    }
}

Mainly, I copied all of the motion logic of my participant. For probably the most half, it really works fairly effectively. Nevertheless, there isn’t any delay, so the get together sprite motion appears uncomfortable (it’s turning into too quick, which is smart because it solely follows the code that I’ve for him). I would like it to look extra a snake.

One other factor with which I had many issues was area. between The Sprite participant and the get together sprite. I used to be attempting to make a brand new preliminary place for the get together sprite, however none of that appeared to assist. I believed I may get the collisions, however I needed to make it possible for I didn’t miss something when utilizing this methodology.

I obtained the code on the backside of this reply:
Does the 2D match comply with the chief within the unit?

This reply helped me drastically, however I’m nonetheless so confused about what the commentator meant about not making use of the Dequeue if I wish to make area between the participant and the get together sprite.

I recognize the assistance! Thanks!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles