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 /
This question was closed Nov 30, 2014 at 04:16 PM by smirlianos for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by smirlianos · Nov 30, 2014 at 03:22 PM · rotationrigidbody2dvelocitylookatlocal

How can I get local 2d Velocity?

Hello, I have this script that is supposed to be attached to an enemy. Then, the enemy must look at the player and walk onto him. The problem is that the enemy is looking at the player, but he constantly goes up, and not towards the player. Any help??

 function Update() {
     //Pl is the variable for player gameobject.
     var dir = pl.transform.position - transform.position;
     var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

     //walk to player
     rigidbody2D.velocity = new Vector2(0, Mathf.Lerp(0, speed, 0.8f));
 }



EDIT: I found a solution:

 rigidbody2D.velocity = transform.TransformDirection(Vector2(0,Mathf.Lerp(0, speed, 0.8f)));
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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Nov 30, 2014 at 04:06 PM

The -90 probably means you constructed your sprite so that the 'front' of the sprite is facing down when the rotation is (0,0,0). Typically a sprite is constructed so that the front is facing right with rotation (0,0,0), eliminating the '-90'. So you can walk the direction you are facing (down) by changing the last line to:

  rigidbody2D.velocity = -transform.up * speed;
Comment
Add comment · Show 1 · 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 smirlianos · Nov 30, 2014 at 04:15 PM 0
Share

$$anonymous$$y sprite is wrong, I have the "-90" because it actually walks on his right side, in front.

avatar image
0

Answer by static_cast · Nov 30, 2014 at 03:52 PM

Use rigidbody2D.velocity.magnitude to read the veolcity.

Use Vector2.Lerp(transform.position, yourPlayer.transform.position, 0.5f); to lerp towards the player.

Comment
Add comment · Show 1 · 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 smirlianos · Nov 30, 2014 at 03:57 PM 0
Share

It doesn't work. It behaves... strangely!

Follow this Question

Answers Answers and Comments

27 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

Related Questions

Unity C# 2D Adding Velocity on rotation 1 Answer

Movement Relative to Rotated Parent 1 Answer

rotate 2d object without affecting the rigidbody ? 1 Answer

Projetil rotation in straight line trajectory 2 Answers

2D rigidbody velocity relative to rotation? 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