10.8 C
New York
Friday, March 14, 2025

unity – Easy Celebration/Participant Motion for Prime-Down RPG


So, to maintain it brief, I’m growing a easy top-down RPG. The participant (and celebration) can solely transfer in 4 instructions. Appears actually easy, however I’ve not been capable of finding a great reply on tips on how to set up celebration motion 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("Velocity", 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 from a web-based tutorial and I believed it was nice! It really works positive. the one subject I’ve is when I’m making the participant stroll in a single course and press a unique arrow-key, they level in that course? I believed the (Mathf.Abs(motion.x) > Mathf.Abs(motion.y)) would additionally stop that however I suppose I used to be mistaken. I’ve not discovered a straight reply on this both. I’ve tried this methodology:

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

        motion.y = 0; // Disable vertical enter if a horizontal secret is pressed

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

However this didn’t assist. It did not appear to vary something. It’s purported to negate the participant character from detecting one other arrow-key motion.

Now, onto the celebration subject.
Initially, I believed I might simply copy-paste the whole lot from the participant character and simply ensure that to modify issues round, however that gave the impression to be tougher than I believed.
That is the code for my celebration 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 avoid 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("Velocity", 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.Depend > steps) {
            this.remodel.place = report.Dequeue();
        }

    }
}

I basically did copy all the motion logic from my participant. For essentially the most half, it really works fairly nicely. Nonetheless, there isn’t any delay, so the motion from the celebration sprite appears to be like awkward (it’s turning too shortly for instance, which is smart since it’s simply following the code I’ve for it). I need it to look extra like a snake.

One other factor that I had a number of hassle with was the spacing between the participant sprite and the celebration sprite. I used to be making an attempt to make a brand new preliminary place for the celebration sprite however none of it appeared to assist. I believed I might simply mess with the collisions, however I needed to verify I wasn’t lacking something when utilizing this methodology.

I received the code on the backside from this response:
2D celebration comply with the chief in Unity?

This reply helped me tremendously, however I’m nonetheless so confused on what the commenter meant with reference to not making use of the dequeue if I need to make area between the participant and celebration sprite.

I admire the assistance! Thanks!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles