Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Feb 05, 2019 at 07:58 PM by polan31 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by polan31 · Oct 19, 2018 at 11:11 PM · accelerometerface

How to flip an object ?

UNITY 2D C#

I try to turn the face of the object to the right when it moves to the right and to the left, when it moves to the left.

How to do it?

I tried something like that, but my object, apart from rotating left and right, when it has to look right, also turns upside down.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class AccelerometrInput : MonoBehaviour {
 
     public bool lookleft = true;
 
 
     public float speed = 10.0F;
     void Update() {
 
 
 
         Vector3 dir = Vector3.zero;
         dir.x = Input.acceleration.x;
         dir.z = 0;
         if (dir.sqrMagnitude > 1)
             dir.Normalize ();
 
         dir *= Time.deltaTime;
         transform.Translate (dir * speed);
 
         if (dir.x < 0 && !lookleft || dir.x > 0 && lookleft) {
             Flip ();
         }
 
     
     }
 
     void Flip (){
         lookleft = !lookleft;
         Vector3 charscale = transform.localScale;
         charscale *= -1;
         transform.localScale = charscale;
     }
 
 }
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 polan31 · Feb 05, 2019 at 07:57 PM

Solved :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveScript : MonoBehaviour
 {
     
     bool facingLeft = true;
 
 private Rigidbody2D rigid;
 private Vector2 movement;
 public float movementSpeed = 10f;
 
 void Start()
 {
     rigid = GetComponent<Rigidbody2D>();
 }
 
 void Update()
 {
     movement = new Vector2(Input.acceleration.x , 0) * movementSpeed;
     rigid.AddForce(movement);
 }
 
 void LateUpdate (){
 
     Vector3 localScale = transform.localScale;
     if (Input.acceleration.x < -0.1)
     {
         facingLeft = true;
     }
     else if (Input.acceleration.x > 0.1 )
     {
         facingLeft = false;
     }
     if (((facingLeft ) && (localScale.x < 0 )) || ((!facingLeft) && (localScale.x > 0 ))) {
         localScale.x *= -1;
     }
     transform.localScale = localScale;
 
 }
 }

Comment
Add comment · 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
0

Answer by mjdb3d · Oct 20, 2018 at 06:59 AM

Change this

charscale *= -1;

With this

charscale.x *= -1;

Comment
Add comment · Show 4 · 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 polan31 · Oct 20, 2018 at 07:56 AM 0
Share

error CS0019: Operator *=' cannot be applied to operands of type UnityEngine.Vector3' and `UnityEngine.Vector3

avatar image mjdb3d · Oct 20, 2018 at 08:02 AM 0
Share

Sorry for that,now it should be correct.

avatar image polan31 mjdb3d · Oct 20, 2018 at 08:50 AM 0
Share

Okay, now it works.

However, I have a problem.

When I reduce the object on a scale, for example it is too big, then this script does not work anymore because it smears the object.

Any suggestions?

I tried to reduce the value of Vector 3 in the script, but it does not work....

avatar image mjdb3d polan31 · Oct 20, 2018 at 11:12 AM 0
Share

I tested them both and they works fine:

Scale object direct pass Vector3 for scale

Can you specific what the issue?

Follow this Question

Answers Answers and Comments

93 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mkae Object Face Towards Motion 1 Answer

Unity Facial Recognition with Identification 1 Answer

How to make a block have highlighted lines like it does in minecraft 2 Answers

How to visualise face in ar foundation? 0 Answers

Bag Toss Unity Accelerometer 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