Unity2D - Companion

As you read this, keep in mind that I am new to programming and Unity, so I may be missing some of the terms or tools that Unity has to offer. Please refine your answers in order ELI5. Thanks in advance!

I am currently working on some physics of the game for a small personal project. I have currently created the platform, the character, and what should be the next companion.

However, since I'm still not at the same level where I can create perfect code, I found the "enemy" script and tried to modify it a bit. It works to the limit, but it needs some tweaks which I hope I can help you guys with.

This is how it looks now (orange square - companion)

enter image description here

This follows the player and I can adjust the speed to match the companion, not the player. However, as shown in the figure, the companion works for the center of the turntable. What I want to create is a companion that follows the player, but still keeps a small gap from the player.

My first thoughts were to create some kind of constant offset, but I can't figure out how to do this without messing up the follow function.

Hope you can help me, it will be greatly appreciated!

Here is the code for reference.

Code attached to Player:

using UnityEngine;
using System.Collections;

public class PlayerCompanion : MonoBehaviour
{

    //In the editor, add your wayPoint gameobject to the script.
    public GameObject wayPoint;

    //This is how often your waypoint position will update to the player position
    private float timer = 0.5f;

    void Update ()
    {
        if (timer > 0) {
            timer -= Time.deltaTime;
        }
        if (timer <= 0) {
            //The position of the waypoint will update to the player position
            UpdatePosition ();
            timer = 0.5f;
        }
    }

    void UpdatePosition ()
    {
        //The wayPoint position will now be the player current position.
        wayPoint.transform.position = transform.position;
    }
}

      

Code attached to companion:

using UnityEngine;
using System.Collections;

public class FollowerOffset : MonoBehaviour {

    //You may consider adding a rigid body to the zombie for accurate physics simulation
    private GameObject wayPoint;

    private Vector3 wayPointPos;

    //This will be the zombie speed. Adjust as necessary.
    private float speed = 10.0f;

    void Start ()
    {
        //At the start of the game, the zombies will find the gameobject called wayPoint.
        wayPoint = GameObject.Find("wayPoint");
    }

    void Update ()
    {
        wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, wayPoint.transform.position.z);
        //Here, the zombie will follow the waypoint.
        transform.position = Vector3.MoveTowards(transform.position, wayPointPos, speed * Time.deltaTime);
    }

}

      

A blow, probably? :)

+3


source to share


2 answers


You can use a smooth continuation script. I created a sample class for you. This class has functions to follow any given game object with some delay and offset. You will need to adjust some of the values ​​to suit your needs.

using UnityEngine;
using System.Collections;

public class PlayerCompanion : MonoBehaviour
{
    [SerializeField]
    private GameObject wayPoint;
    [SerializeField]
    public Vector3 offset;
    public Vector3 targetPos;//Edit: I forgot to declare this on firt time
    public float interpVelocity;
    public float cameraLerpTime = .1f;
    public float followStrength = 15f;

    // Use this for initialization
    void Start ()
    {
        //At the start of the game, the zombies will find the gameobject called wayPoint.
        wayPoint = GameObject.Find("wayPoint");
        offset = new Vector3 (5,0,0);//input amount of offset you need
    }

    void FixedUpdate () {
        if (wayPoint) {
            Vector3 posNoZ = transform.position;

            Vector3 targetDirection = (wayPoint.transform.position - posNoZ);
            interpVelocity = targetDirection.magnitude * followStrength;
            targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime); 

            transform.position = Vector3.Lerp (transform.position, targetPos + offset, cameraLerpTime);
        }

    }
}

      



Attach this class to your companion, play with different values.

+2


source


To preserve the orientation of the object, your companion cannot be a child of your main character.

Your wayPoint shouldn't be a GameObject but a Transform instead and your code will look better.

If your game is a 2D platform, your and your interlocutor should be back to your player, he can only apply one axis (X?) So that you can quickly reduce your waiPoint by calculating it on your UpdatePosition function, for example this:

wayPoint.position = transform.position * (Vector3.left * distance);

      



where your "distance" can be public to customize easily.

so on your companion script Update just do:

transform.position = Vector3.MoveTowards(transform.position, wayPoint.position, speed * Time.deltaTime);

      

I can't verify this right now, so you might have problems with multiple Vector3 operations, just a comment and I'll try to fix it as best I can;)

0


source







All Articles