• 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 KryChaos · Nov 25, 2015 at 08:57 PM · scripting problemscripting beginner

Difficulty Moving Sprite left and right when walking left and right.

I want the sprite to look left and right when walking but the script wont work.

using UnityEngine; using System.Collections;

public class player : MonoBehaviour {

 public float maxSpeed = 3;
 public float speed = 50f;
 public float jumpPower = 150f;
 
 public bool grounded;
 
 private Rigidbody2D rb2d;
 private Animator anim;
 
 void Start () 
 {
     rb2d = gameObject.GetComponent<Rigidbody2D> ();
     anim = gameObject.GetComponent<Animator> ();

     if (Input.GetAxis ("Horizontal") < -0.1f) {
         transform.localScale = new Vector3 (-1, 1, 1);
     }

     if (Input.GetAxis ("Horizontal") > 0.1f) {
         transform.localScale = new Vector3(1, 1, 1);
     }

         

 }
 
 
 void Update () 
 {
     
     anim.SetBool ("Grounded", grounded);
     anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis("Horizontal")));
     
 }
 
 void FixedUpdate()
 {
     float h = Input.GetAxis("Horizontal");
     
     
     rb2d.AddForce((Vector2.right * speed) * h);
     
     if (rb2d.velocity.x > maxSpeed) {
         rb2d.velocity = new Vector2 (maxSpeed, rb2d.velocity.y);
     }
     
     if (rb2d.velocity.x < -maxSpeed) {
         rb2d.velocity = new Vector2 (-maxSpeed, rb2d.velocity.y);
     }
     
     
 }
 

}

Comment
Add comment · Show 4
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 timdouglas90 · Nov 25, 2015 at 11:43 PM 1
Share

What's not working about it? Need a bit more info since the code looks a bit weird...I'd have done something like this...

 using UnityEngine; 
 using System.Collections;
 
 public class player : $$anonymous$$onoBehaviour {
 
  public float maxSpeed = 3;
  public float speed = 50f;
  public float jumpPower = 150f;
  
  public bool grounded;
  
  private Rigidbody2D rb2d;
  private Animator anim;
  
  void Start () 
  {
      rb2d = gameObject.GetComponent<Rigidbody2D> ();
      anim = gameObject.GetComponent<Animator> ();
      if (Input.GetAxis ("Horizontal") < -0.1f) {
          transform.localScale = new Vector3 (-1, 1, 1);
      }
      if (Input.GetAxis ("Horizontal") > 0.1f) {
          transform.localScale = new Vector3(1, 1, 1); 
          //Not sure why you set a vector 3 
          //since you only need a vector 2 for 2D...
          //Also not sure why you set them to 1?
      }
          
  }
  
  
  void Update () 
  {
      
      anim.SetBool ("Grounded", grounded);
      anim.SetFloat("Speed", $$anonymous$$athf.Abs(Input.GetAxis("Horizontal")));
      
  }
  
  void FixedUpdate()
  {
      float h = Input.GetAxis("Horizontal");
      
      if(Input.GetAxis("Horizontal") < 0)
      {
         rb2d.AddForce(Vector2.left * speed);
      }
 
      if(Input.GetAxis("Horizontal") > 0)
      {
         rb2d.AddForce(Vector2.right * speed);
      }
 
      if(Input.GetAxis("Horizontal") == 0)
      {
         rb2d.velocity = 0;
      }
  }
  
 }
avatar image KryChaos · Nov 27, 2015 at 11:09 AM 0
Share

When i walk left, i want the sprite to look left, and right when walking right.

avatar image timdouglas90 KryChaos · Nov 27, 2015 at 07:33 PM 0
Share

Ah in that case then you'll need a simple bool for isFacingRight and then set the local transform scale to positive and negative values on the x axis like so...

 using UnityEngine;
 using System.Collections;
 
     public class Player : $$anonymous$$onoBehaviour
     {
         private Rigidbody2D rb2d;
         private bool isFacingRight;
 
         void Start()
         {
             rb2d = GetComponent<Rigidbody2D>();
         }
 
         void FixedUpdate() 
         {
             float h = Input.GetAxis("Horizontal");
               
               //move LEFT
              if(Input.GetAxis("Horizontal") < 0)
               {
                  rb2d.AddForce(Vector2.left * speed * Time.Deltatime);
                  if(isFacingRight)
                  {
                      Flip();
                  }
               }
              
              //move RIGHT
               if(Input.GetAxis("Horizontal") > 0)
               {
                  rb2d.AddForce(Vector2.right * speed * Time.Deltatime);
                  if(!isFacingRight)
                  {
                      Flip();
                  }
               }
  
              //STOP moving
             if(Input.GetAxis("Horizontal") == 0)
               {
                  rb2d.velocity = 0;
               }
         }
 
         void Flip()
         {
             transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
             _isFacingRight = transform.localScale.x > 0;
         }
     }


NOTE: This is untested code so use with care, I lifted Flip() straight from my own character controller :)

avatar image LazyElephant · Nov 28, 2015 at 02:58 AM 0
Share

It looks like the main problem is that all your code to handle the sprite facing is in your Start () function, which only runs once. Try moving it to your update or fixed update.

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make an object a certain distance from me. 2 Answers

Linking Image to item list 1 Answer

Selecting an ability based on UI image/button, and thereafter selecting a target for the ability 0 Answers

Random switch enum with a delay 1 Answer

(URGENT) I have a swinging axe I can get to rotate once in 1 direction but i need it to loop 3 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