• 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 /
  • Help Room /
avatar image
0
Question by z3422393 · Sep 06, 2015 at 02:39 AM · localscale

Problem with transform.localScale

Hi, I'm trying to flip my sprite every time it changes direction. I've been trying to solve this issue for the past couple of hours. This is the code I'm using:

 private Vector3 scale;
 void Start(){
 scale = new Vector3 (-1f,1f,1f);
 }
 void Update(){
 if (moveSpeed < 0){
 transform.localScale = scale;
 }

The moveSpeed changes each time it collides with a wall. It seems to collide and change speed perfectly fine, but it will not flip the character

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 MakeCodeNow · Sep 06, 2015 at 02:52 AM

It's hard to tell from you code, but generally speeds will never be less than zero. You may need to check if moveSpeed < 0.001 or something. You can put a breakpoint in that code to see what's actually happening.

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 z3422393 · Sep 06, 2015 at 03:12 AM 0
Share

When the player collides with the wall, it sets the speed to a negative value. I also tried putting the "transform.localScale = scale;" in the same bit of code where the speed is changed. Here is the code in its entirety:

 using UnityEngine;
 using System.Collections;
 
 public class playercontroller : $$anonymous$$onoBehaviour {
 
 
     private Rigidbody2D rb;
     private float velocityX;
     private Collider2D myCollider;
 
     public bool isGround;
     public bool touchingWall;
     public float moveSpeed;
     public float jumpHeight;
     public Layer$$anonymous$$ask whatIsGround;
     public Layer$$anonymous$$ask whatIsWall;
     public Transform groundCheck;
     public Transform wallCheck;
     public float groundCheckRadius;
     public float wallTouchRadius;
     public int playerHealth;
     private Vector3 scale;
 
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody2D> ();    
         scale = new Vector3 (-1f, 1f, 1f);
     }
 
     void FixedUpdate(){
         isGround = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
         touchingWall = Physics2D.OverlapCircle (wallCheck.position, wallTouchRadius, whatIsWall);
         if (moveSpeed <= 0) {
             transform.localScale = scale;
         }
     }
     // Update is called once per frame
     void Update () {
         rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
 
         if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && (isGround || touchingWall)){
             rb.velocity = new Vector2 (rb.velocity.x, jumpHeight);
         }
     }
 
 
     void OnCollisionEnter2D (Collision2D col){
         if (col.gameObject.tag == "spike") {
             playerHealth -= 10;
         }
         if (col.gameObject.tag == "wall" && moveSpeed > 0) {
             moveSpeed = -moveSpeed;
         }else 
         if (col.gameObject.tag == "wall" && moveSpeed < 0) {
             moveSpeed = 8;
         }
         if (playerHealth <= 0) {
             Destroy(gameObject);
         }
     }
 }
 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

localScale in network 0 Answers

Change transform on ChildObjects except one 0 Answers

NaN error when trying to adjust Scale as a percentage. 1 Answer

My transform.localScale = Vector3.Lerp isnt working. 0 Answers

How can I make A Object have the same Scale as B Object? 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