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 lloladin · Jun 01, 2015 at 01:22 PM · c#3dlocalscale

Move Torwards Transform.Localscale not working

so i have made a simpel script that makes an enemy move torwards the player it works really well i just have a litle problem which is when i try to Transform.local scale my enemy so that the graphic will flip if hes changing direction it simply dosent work im using the same code that i used to flip my player when i press a A/D to move Right/Left by simply checking wether the Rigidiy body is moving right or left no idea why its not working neither did anyone that i asked and not quite sure what to google but gave a shot with no luck any help is appreciated Ty in advance.

Source Code

 public bool EnemyOnPlayer;
 
     private Rigidbody myrigidbody;
 
     public PlayerController thePlayer;
 
     // Use this for initialization
     void Start () {
         EnemyOnPlayer = false;
 
         myrigidbody = FindObjectOfType<Rigidbody> ();
 
         thePlayer = FindObjectOfType<PlayerController>();
     }
     
     // Update is called once per frame
     void Update () {
         if (EnemyOnPlayer == false)
         {
             transform.position = Vector3.MoveTowards (transform.position, thePlayer.transform.position, Time.deltaTime * move_speed);
         }
 
         
         if (myrigidbody.velocity.x > 0)
         {
             transform.localScale = new Vector3 (1f, 1f, 1f);
         }
         if (myrigidbody.velocity.x < 0)
         {
             transform.localScale = new Vector3 (-1f, 1f, 1f);
         }
     }


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
1
Best Answer

Answer by Gardes · Jun 02, 2015 at 05:01 AM

Vector3.MoveTowards is not adding any physic things, so velocity stays always at 0.

If you still want to move your Object without physics just calculate with your positions.

         if (transform.position.x < thePlayer.transform.position.x)
         {
             transform.localScale = new Vector3 (1f, 1f, 1f);
         }
 
         if (transform.position.x > thePlayer.transform.position.x)
         {
             transform.localScale = new Vector3 (-1f, 1f, 1f);
         }

This is untested pseudo code, just to give you a shot.

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 lloladin · Jun 02, 2015 at 06:12 AM 1
Share

Works like a charm ty mate

avatar image Gardes · Jun 02, 2015 at 11:25 AM 1
Share

Im glad :)

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

21 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

Related Questions

How do i get the LocalSize of an object? 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Crouch Script/ Scaling transform from one side only 1 Answer

How can I create a 3d mesh using raycasts? 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