• 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 ger122 · Dec 29, 2014 at 09:30 PM · physics

Make character face toward MOUSE /Raycast. 2D Left/Right

I have this script but I need the gameobject to flip left when mouse moves to the left and right when mouse moves to the right

-----------THIS IS ALL A 2D GAME / CSHARP SCRIPT------- I do know theres quite a bit of answers for this but none I see are for raycasted gameobjects . My object the script is attached to does flip but only on Key press , I do not know how to code this so it flips on/ with MOUSE.
Again I need this script adjusted so gameobject will flip ON/WITH mouse movement... NOT keypress since it is initially set to move with raycast. code is below

 using UnityEngine;
 using System.Collections;
 
 public class loud2d: MonoBehaviour {
 
         public float maxspeed = 10f;
         bool facingRight = true;
         
         void Start()
         {}
         
         
         void FixedUpdate()
         {
             
             float move = Input.GetAxis ("Horizontal");
                 
                 rigidbody2D.velocity = new Vector2(move * maxspeed, rigidbody2D.velocity.y);
                 
                 
                 
                 if (move > 0 && !facingRight)
                 Flip();
                 
                 else if (move < 0 && facingRight ) 
                 Flip();
                 
             }
             void Flip()
                 
             {
                 
                 
                 facingRight = !facingRight ;
                 Vector3 theScale =transform.localScale;
                 theScale.x *= -1;
                 transform.localScale = theScale;
                 
             }
         }








Comment
Add comment · Show 7
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 Glurth · Dec 29, 2014 at 10:25 PM 0
Share

Quaternion.LookRotation(moveDirection); this function takes a Vector3 as a parameter, not a Vector2. Does this help? Quaternion.LookRotation(new Vector3(moveDirection.x,moveDirection.y,0)); EDIT: this comment was posted in the wrong question- sorry about that! Also: have you tried using Input.GetAxis("$$anonymous$$ouse X"); rather than "Horizontal"

avatar image ger122 · Dec 29, 2014 at 11:52 PM 0
Share

where exactly should I put this in my script and what should I take out if anything? thanks

avatar image ger122 · Dec 30, 2014 at 01:38 PM 0
Share

do i include both statement or just bottom one? and where should I place it?

avatar image ger122 · Dec 30, 2014 at 05:09 PM 0
Share

nice -- worked

avatar image Glurth · Jan 01, 2015 at 06:15 PM 1
Share

Hi Ger122, I converted my comment into an ANSWER. Since that worked, could you please select it as the accepted answer.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Glurth · Dec 30, 2014 at 02:56 PM

Simply replace float move = Input.GetAxis ("Horizontal"); in your code with float move = Input.GetAxis ("Mouse X");

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
Wiki

Answer by Oana · Dec 30, 2014 at 03:19 PM

So you want to make it so that if the mouse pointer is to the left of the object, it looks to the left, and if it's to the right, it faces right?

Something like this maybe to replace the if statements:

 float distanceCameraCharacter = Mathf.Abs(Camera.main.transform.position.z - transform.position.z);
 Vector3 mousePos =  camera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.x, distanceCameraCharacter));
 float diffCharMousePos = mousePos.x - transform.position.x;
 if (diffCharMousePos > 0 && !facingRight)
     Flip();
 else if (diffCharMousePos < 0 && facingRight )
     Flip();


Note: code is not actually checked, let me know if it doesn't work and you can't figure out why.

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 ger122 · Dec 30, 2014 at 05:08 PM 0
Share

yes getting error Assets/loud2d.cs(35,27): error CS0103: The name `Flip' does not exist in the current context

also please keep in $$anonymous$$d this is a 2D GA$$anonymous$$E So does vector 3 work with this?

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

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

2D 360 degress platformer example needed 0 Answers

OnCollision events are not always fired 2 Answers

Rigidbodies get huge amount of velocity when clipping into other Rigidbodies 2 Answers

Fist Passing Through The Target 2 Answers

How is drag applied to force? 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