Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 nikhilVardhan · May 17 at 01:42 AM · scripting problemplayer movement

Character not moving towards desired random location

Hi team,

I know there are similar questions and tried to go with the solution from a couple of them but none worked.

I am trying to move my enemy character towards a random location which I am finding using "Random" method as shown in the script. Now the problem is my character isn't moving at all and is just hovering over one place only in "Running" animation.

I tried print() method to find if "if-else" is not getting skipped and it is not. It is like "transform.position" is not working at all:

Script:

public float minX; public float maxX; public float minY; public float maxY;

 Vector2 targetPosition;

 Animator anim;

 public float stopDistance;

 private float attackTime;

 public float attackSpeed;

 public Enemy enemyToSummon;
 public float timeBetweenSummons;

 private float summonTime;


 public override void Start()
 {
     base.Start();
     float randomX = Random.Range(minX, maxX);
     float randomY = Random.Range(minY, maxY);
     targetPosition = new Vector2(randomX, randomY);
     anim = GetComponent<Animator>();
 }


 private void Update()
 {
     if (player != null)
     {

         if ((Vector2)transform.position != targetPosition)
         {
             anim.SetBool("isRunning", true);
             transform.position = Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
         }
         else
         {
                 
             anim.SetBool("isRunning", false);

             if (Time.time >= summonTime)
             {
                 summonTime = Time.time + timeBetweenSummons;
                 anim.SetTrigger("summon");
             }

         }

         if (Vector2.Distance(transform.position, player.position) <= stopDistance)
         {
             if (Time.time >= attackTime)
             {
                 attackTime = Time.time + timeBetweenAttacks;
                 StartCoroutine(Attack());
             }
         }
     }
 }



alt text

Script is attached to Summoner empty object which inherits sprites of different parts of enemy.

screenshot-8.png (304.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by landings · May 17 at 02:31 AM

Maybe your speed is too small. You can print speed * Time.deltaTime and check your delta distance.

Comment
Add comment · Show 2 · Share
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
avatar image nikhilVardhan · May 18 at 07:51 AM 0
Share

Hi, that is not the case I increased y speed tp 500 from 15 and it is still the same.

avatar image landings nikhilVardhan · May 19 at 02:10 AM 0
Share

I can't think of anything specific other than this. You may have to log more like how much is targetPosition - transform.position, or what is the result of Vector2.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);, or how does transform.position change before and after this assignment, or is it altered by other scripts elsewhere.

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

244 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

Related Questions

Cannot Rotate Player after Matching Camera Rotation? 0 Answers

transform.forward not always forward? 2 Answers

how to disable player inputs? 1 Answer

[New Input System] Player Rotation & Cursor Following Mouse,[New Input System] Player Rotation & Cursor Object Following Mouse 0 Answers

Camera script for ball not working as intended, looking for some tips, or advise. 0 Answers


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