So, to maintain it quick, I’m creating a easy top-down RPG. The participant (and social gathering) can solely transfer in 4 instructions. Appears actually easy, however I’ve not been capable of finding a superb reply on the right way to set up social gathering motion and I believe 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 assumed it was nice! It really works positive. the one difficulty I’ve is when I’m making the participant stroll in a single course and press a distinct arrow-key, they level in that course? I assumed the (Mathf.Abs(motion.x) > Mathf.Abs(motion.y)) would additionally forestall that however I assume 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 key's 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 presupposed to negate the participant character from detecting one other arrow-key motion.
Now, onto the social gathering difficulty.
Initially, I assumed I may simply copy-paste all the things from the participant character and simply be sure that to change issues round, however that gave the impression to be more durable than I assumed.
That is the code for my social gathering 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 steer clear of 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.rework.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.rework.place = report.Dequeue();
}
}
}
I basically did copy all the motion logic from my participant. For probably the most half, it really works fairly effectively. Nonetheless, there isn’t any delay, so the motion from the social gathering sprite seems awkward (it’s turning too rapidly for instance, which is smart since it’s simply following the code I’ve for it). I need it to appear extra like a snake.
One other factor that I had quite a lot of bother with was the spacing between the participant sprite and the social gathering sprite. I used to be attempting to make a brand new preliminary place for the social gathering sprite however none of it appeared to assist. I assumed I may simply mess with the collisions, however I wished to verify I wasn’t lacking something when utilizing this methodology.
I acquired the code on the backside from this response:
2D social gathering 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 wish to make house between the participant and social gathering sprite.
I respect the assistance! Thanks!