Skip to content

ThirteeNov

My personal blog about coding and internet

Menu
  • About me
  • About Zofia Kreasi
  • Cart
  • Checkout
  • Making an airplane game from scratch in Unity
  • My account
  • Privacy Policy
  • Privacy Policy – zkLeaderboard
  • Sample Page
  • Shop
  • Tutorials on Learning JavaScript
  • ZKAccounts – Privacy Policy
Menu

How to make “Friend Follows Player” in Unity Game Development

Posted on July 10, 2023July 10, 2023 by Habibie

This is a video tutorial showing you how to make a friendly object/character that will follow the player object anywhere it goes. For example in this game I have FPS character controller, and wherever I go I can make that friendly object walks following me (the player).

This is the script that I used in this video, I call it FriendMover.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FriendMover : MonoBehaviour
{
	
	public string playerName = "PlayerName";
	public float speed = 3f;
	public float distance = 5f;
    GameObject player;
    Animator anim;
    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find(playerName);
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
		
		if(Vector3.Distance(transform.position, player.transform.position) < distance){
			anim.SetBool("isMoving", false);
		}else{
			anim.SetBool("isMoving", true);
			Quaternion targetRotation = Quaternion.LookRotation(player.transform.position - transform.position);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, 3 * Time.deltaTime);
            transform.position += transform.forward * speed * Time.deltaTime;
		}
		
    }

}
Post Views: 344
ciihuy2020

Welcome!

  • My YouTube Channel
  • My GitHub Page
  • About me

Categories

  • 3DVista
  • Android
  • Apache
  • C#
  • Cordova
  • Electron & Node JS
  • HTML5, CSS & JavaScript
  • iOS
  • Let's Make Unity Games
  • Misc
  • Photoshop
  • PHP
  • Python
  • Uncategorized
  • Unity
  • WordPress

Recent Posts

  • Make objects like wires and cables easily in Unity using Ciihuy Curved Mesh
  • [SOLVED] Can’t Add Custom Domain to Blogger After Losing CNAME Verification
  • iOS App Icon Generator by CiihuyCom
  • Advanced Blinking Marker Script to show objects position in your game canvas
  • Ciihuy Images Merger – Fast & Easy Online Image Combiner
© 2025 ThirteeNov | Powered by Superbs Personal Blog theme