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 Sep 20, 2018 at 03:32 AM by CSandoval for the following reason:

I figured it out

avatar image
0
Question by CSandoval · Sep 19, 2018 at 08:21 PM · rotationcollisioncharactercontrollerfacinginvert

How to change the direction the player is facing when colliding with an object

So I am still very new to unity and I am having trouble with some code I have that changes the direction the character model is facing so that it faces in the direction the player is moving. What I'm having trouble with in particular is that I have a reverse gravity zone and for whatever reason when the player enters it it reverses not only the gravity, but also the player's controls so I need to switch it back to normal. I figured out how to change the controls, but the character now faces the wrong way while moving and I have no clue how to fix it.

 using UnityEngine;
 using System.Collections;
  
  public class Adjust2dFacing : MonoBehaviour
  {
      public CheckType inputType = CheckType.InputAxis;
      public float axisThreshold = 0.2f;
      public KeyCode inputKeyLeft = KeyCode.A;
      public KeyCode inputKeyRight = KeyCode.D;
  
      private bool isFacingRight = true;
  
      public enum CheckType
      {
          InputAxis,
          InputKeys
      }
      
      // Update is called once per frame
      void Update ()
      {
          if(inputType == CheckType.InputAxis) // check axis input
          {
              float xInput = Input.GetAxis("Horizontal");
  
              if(!isFacingRight && xInput > axisThreshold) // facing left - turn right
              {
                  transform.Rotate(0, 180, 0);
              }
              else if(isFacingRight && xInput < -axisThreshold) // facing right - turn left
              {
                  transform.Rotate(0, 180, 0);
              }
          }
          else if(inputType == CheckType.InputKeys) // check key input
          {
              if(!isFacingRight && Input.GetKey(inputKeyRight)) // facing left - turn right
              {
                  transform.Rotate(0, 180, 0);
              }
              else if(isFacingRight && Input.GetKey(inputKeyLeft)) // facing right - turn left
              {
                  transform.Rotate(0, 180, 0);
              }
          }
  
          // update facing direction
          isFacingRight = (transform.position.x + (transform.forward * 5).x) > transform.position.x;
      }
  
      void OnTriggerEnter(Collider other)
      {
          if (other.tag == "Reverse")
          {
              inputKeyLeft = KeyCode.D;
              inputKeyRight = KeyCode.A;
              transform.Rotate(0, 180, 0);
          }
      }
  }
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

Answer by tormentoarmagedoom · Sep 19, 2018 at 10:31 PM

Good day.

How are you moving the player? I think you are overthinking one simple problem.

You must know you can move and rotate objects in soo many ways. But in particular for this case, you must know you can use global axis, i mean static absloute axis of unity, or use the relative axis from the object. (when it rotates 180º, its relative axis rotates with it).

I think you are moving the player with its local rotation, but the camera is not rotating with it, so when object rotates, it becomes "reversed". the most easy way is to change the commands you use for moving.

Maybe this is better for you:

https://docs.unity3d.com/ScriptReference/Vector3.Lerp.html https://docs.unity3d.com/ScriptReference/Transform-position.html

changing it directly, and use lerp to smooth the movement

Bye!

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 CSandoval · Sep 19, 2018 at 11:50 PM 0
Share

Oh ya I forgot some important information, the player is moving with a character controller and this is for a 2D platformer in unity 3D.

avatar image
0

Answer by ahstein · Sep 20, 2018 at 12:52 AM

Is your character a sprite or 3d mesh?

If it is a sprite, are you aware of the flip property of the Sprite Renderer Component? If you just set flipX to true, you sprite will be mirrored along the y axis without having to worry about the weirdness of rotating the transform.

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

Follow this Question

Answers Answers and Comments

174 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 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

CharacterController "breaks" collision, goes haywire 2 Answers

How to get the relative position of a Collider with OnControllerColliderHit()? 2 Answers

Character Controller change target direction in degree 1 Answer

CharacterController & Particles 1 Answer

Problem with rigidbodys and box collider 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