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 Mikela · Nov 10, 2014 at 08:53 PM · movecharacter motormoves

How to make character run to enemy?

Hi,

I have a bug in my script. When player press skill key but he is not in range to attack character is starting to run to enemy to attack, but when enemy is starting to move character is getting crazy and running in random directions.

Can some one help me?

Method movo to enemy:

 if (player.oponent != null)
         {
             if (runtToEnemy)
             {
                 //Char moving
                 if (Vector3.Distance(transform.position, player.oponent.transform.position) > 3)
                 {
                     Quaternion newRotation = Quaternion.LookRotation(player.oponent.transform.position - transform.position, Vector3.forward);
                     newRotation.x = 0f;
                     newRotation.z = 0f;
                     Debug.Log(newRotation);
                     transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 20);
                     playerMove.controller.SimpleMove(transform.forward * playerMove.speed);
                     animation.CrossFade(playerMove.run.name);
 
                     if (Vector3.Distance(transform.position, player.oponent.transform.position) <= range)
                     {
                         runtToEnemy = false;
                         player.resetAttackFunction();
                         player.specialAttack = true;
                         inAction = true;
                         skillKey = false;
                     }
                 }
                 //Char not moveing
                 else
                 {
                     animation.CrossFade(playerMove.idle.name);
                 }
             }
         }


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
Wiki

Answer by Lysander · Nov 11, 2014 at 07:00 AM

99% of the time you won't be setting the w/x/y/z values of a quaternion directly, both because there's no need to and because it'll almost always end up making things worse (those values don't work how most people think they do). You're also setting the "upwards" value to "forward" in the LookRotation function, which depending on how the game works may be correct, but I can't tell from here.

My suggestion is to use the transform.LookAt(player.oponent.transform.position) function and skip all of this complicated-ness entirely, but it seems that you want to slowly change the rotation of the player as you're moving. Try cutting out the Vector3.forward from the LookRotation entirely, and then cut the 0f assignments to x and z and see if that works. These things are usually a lot simpler than people make them.

Another problem may be that you're using the local-space direction in a world-space function (SimpleMove). In order to fix this, you simply need to use the TransformDirection function on the player object's transform, and specify which local direction is "forward". Can't test it myself, but consider the following changes:

  if (player.oponent != null)
          {
              if (runtToEnemy)
              {
                  //Char moving
                  if (Vector3.Distance(transform.position, player.oponent.transform.position) > 3)
                  {
                      Quaternion newRotation = Quaternion.LookRotation(player.oponent.transform.position - transform.position, Vector3.forward);
                      Debug.Log(newRotation);
                      transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 20);
                      
 
                      Vector3 forwardDirection = transform.TransformDirection(Vector3.forward);
                      playerMove.controller.SimpleMove(forwardDirection * playerMove.speed);
                      animation.CrossFade(playerMove.run.name);
  
                      if (Vector3.Distance(transform.position, player.oponent.transform.position) <= range)
                      {
                          runtToEnemy = false;
                          player.resetAttackFunction();
                          player.specialAttack = true;
                          inAction = true;
                          skillKey = false;
                      }
                  }
                  //Char not moveing
                  else
                  {
                      animation.CrossFade(playerMove.idle.name);
                  }
              }
          }
Comment
Add comment · Show 3 · 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 Mikela · Nov 11, 2014 at 07:16 AM 0
Share

I'm creating click to move (with camera looking from top) game. I have tried to remove quaternion ant use transform.LookAt() function. But it still not helped. So the problem is this line:

player$$anonymous$$ove.controller.Simple$$anonymous$$ove(transform.forward * player$$anonymous$$ove.speed);

avatar image Lysander · Nov 11, 2014 at 05:02 PM 0
Share

If that's the case, the problem might be with the exchange from local to world spatial directions. Ins$$anonymous$$d of calling Simple$$anonymous$$ove with the transform.forward, try assigning the forward direction from the transform (local space) into a vector3 in the world space using transform.TransformDirection(localspace direction) and then use that for the Simple$$anonymous$$ove function ins$$anonymous$$d. I'm nearly late for work and can't really write out code right now, but the unity docs should have the proper formatting.

avatar image Lysander · Nov 11, 2014 at 10:21 PM 0
Share

Added to the answer. Let me know if it works ^_^

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The most effective way to search enemies. 1 Answer

C# move main camra via mouse position on boarders around screen script for RTS Game 0 Answers

translate 2 Answers

Make your player not move during a animation? 0 Answers

move character controller forward using gui button. 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