Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AbhiramSankar102000 · Jul 25, 2021 at 09:33 PM · 2d2d game2d-platformer2d sprites

Making the enemy follow the path of the Player,How to make the enemy follow the path of the player?

So I have been trying to make the Enemy GameObject follow the Player GameObject using this code below. But this code doesn't allow the enemy to follow the path of the player and gets stuck in the level...

alt text

Any modifications to code I can make to allow the enemy to follow the path taken by the player?

PlayerFollower.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerFollower : MonoBehaviour
 {
     [Header("Settings")]
     [Tooltip("The speed at which the enemy moves.")]
     public float moveSpeed = 5.0f;
     [Header("Following Settings")]
     [Tooltip("The transform of the object that this enemy should follow.")]
     public Transform followTarget = null;
     [Tooltip("The distance at which the enemy begins following the follow target.")]
     public float followRange = 10.0f;
     public enum MovementModes { NoMovement, FollowTarget, Scroll };
 
     [Tooltip("The way this enemy will move\n" +
         "NoMovement: This enemy will not move.\n" +
         "FollowTarget: This enemy will follow the assigned target.\n" +
         "Scroll: This enemy will move in one horizontal direction only.")]
     public MovementModes movementMode = MovementModes.FollowTarget;
 
     //The direction that this enemy will try to scroll if it is set as a scrolling enemy.
     [SerializeField] private Vector3 scrollDirection = Vector3.right;
     /// <summary>
     /// Description:
     /// Standard Unity function called after update every frame
     /// Inputs: 
     /// none
     /// Returns:
     /// void (no return)
     /// </summary>
     private void LateUpdate()
     {
         HandleBehaviour();
     }
 
     /// <summary>
     /// Description:
     /// Standard Unity function called once before the first call to Update
     /// Input:
     /// none
     /// Return:
     /// void (no return)
     /// </summary>
     private void Start()
     {
         if (movementMode == MovementModes.FollowTarget && followTarget == null)
         {
             if (GameManager.instance != null && GameManager.instance.player != null)
             {
                 followTarget = GameManager.instance.player.transform;
             }
         }
     }
 
     /// <summary>
     /// Description:
     /// Handles moving and shooting in accordance with the enemy's set behaviour
     /// Inputs:
     /// none
     /// Returns:
     /// void (no return)
     /// </summary>
     private void HandleBehaviour()
     {
         // Check if the target is in range, then move
         if (followTarget != null && (followTarget.position - transform.position).magnitude < followRange)
         {
             MoveEnemy();
         }
     }
 
     /// <summary>
     /// Description:
     /// Moves the enemy and rotates it according to it's movement mode
     /// Inputs: none
     /// Returns: 
     /// void (no return)
     /// </summary>
     private void MoveEnemy()
     {
         // Determine correct movement
         Vector3 movement = GetDesiredMovement();
 
         // Determine correct rotation
         Quaternion rotationToTarget = GetDesiredRotation();
 
         // Move and rotate the enemy
         transform.position = transform.position + movement;
         transform.rotation = rotationToTarget;
     }
 
     /// <summary>
     /// Description:
     /// Calculates the movement of this enemy
     /// Inputs: 
     /// none
     /// Returns: 
     /// Vector3
     /// </summary>
     /// <returns>Vector3: The movement of this enemy</returns>
     protected virtual Vector3 GetDesiredMovement()
     {
         Vector3 movement;
         switch (movementMode)
         {
             case MovementModes.FollowTarget:
                 movement = GetFollowPlayerMovement();
                 break;
             case MovementModes.Scroll:
                 movement = GetScrollingMovement();
                 break;
             default:
                 movement = Vector3.zero;
                 break;
         }
         return movement;
     }
 
     /// <summary>
     /// Description:
     /// Calculates and returns the desired rotation of this enemy
     /// Inputs: 
     /// none
     /// Returns: 
     /// Quaternion
     /// </summary>
     /// <returns>Quaternion: The desired rotation</returns>
     protected virtual Quaternion GetDesiredRotation()
     {
         Quaternion rotation;
         switch (movementMode)
         {
             case MovementModes.FollowTarget:
                 rotation = GetFollowPlayerRotation();
                 break;
             case MovementModes.Scroll:
                 rotation = GetScrollingRotation();
                 break;
             default:
                 rotation = transform.rotation; ;
                 break;
         }
         return rotation;
     }
 
     /// <summary>
     /// Description:
     /// The direction and magnitude of the enemy's desired movement in follow mode
     /// Inputs: 
     /// none
     /// Returns: 
     /// Vector3
     /// </summary>
     /// <returns>Vector3: The movement to be used in follow movement mode.</returns>
     private Vector3 GetFollowPlayerMovement()
     {
         Vector3 moveDirection = (followTarget.position - transform.position).normalized;
         Vector3 movement = moveDirection * moveSpeed * Time.deltaTime;
         return movement;
     }
 
     /// <summary>
     /// Description
     /// The desired rotation of follow movement mode
     /// Inputs: 
     /// none
     /// Returns: 
     /// Quaternion
     /// </summary>
     /// <returns>Quaternion: The rotation to be used in follow movement mode.</returns>
     private Quaternion GetFollowPlayerRotation()
     {
         float angle = Vector3.SignedAngle(Vector3.down, (followTarget.position - transform.position).normalized, Vector3.forward);
         Quaternion rotationToTarget = Quaternion.Euler(0, 0, angle);
         return rotationToTarget;
     }
 
     /// <summary>
     /// Description:
     /// The direction and magnitude of the enemy's desired movement in scrolling mode
     /// Inputs: 
     /// none
     /// Returns: 
     /// Vector3
     /// </summary>
     /// <returns>Vector3: The movement to be used in scrolling movement mode.</returns>
     private Vector3 GetScrollingMovement()
     {
         scrollDirection = GetScrollDirection();
         Vector3 movement = scrollDirection * moveSpeed * Time.deltaTime;
         return movement;
     }
 
     /// <summary>
     /// Description
     /// The desired rotation of scrolling movement mode
     /// Inputs: 
     /// none
     /// Returns: 
     /// Quaternion
     /// </summary>
     /// <returns>Quaternion: The rotation to be used in scrolling movement mode</returns>
     private Quaternion GetScrollingRotation()
     {
         return Quaternion.identity;
     }
 
     /// <summary>
     /// Description:
     /// Determines the direction to move in with scrolling movement mode
     /// Inputs: 
     /// none
     /// Returns: 
     /// Vector3
     /// </summary>
     /// <returns>Vector3: The desired scroll direction</returns>
     private Vector3 GetScrollDirection()
     {
         Camera camera = Camera.main;
         if (camera != null)
         {
             Vector2 screenPosition = camera.WorldToScreenPoint(transform.position);
             Rect screenRect = camera.pixelRect;
             if (!screenRect.Contains(screenPosition))
             {
                 return scrollDirection * -1;
             }
         }
         return scrollDirection;
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

308 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help Me! I want to make a pixel fairy! 1 Answer

Pixelart distorts on import. 1 Answer

How do I stop my sprite from jumping in the air? 1 Answer

Instantiate a GameObject with a specific Z rotation 2 Answers

2D Sprite Fillup without using UI 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges