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 /
avatar image
0
Question by emilija69 · Mar 22, 2020 at 06:29 PM · triggerscontrolsendless runnerturn

Triggers and Controls not working after player turns in endless runner

I am new at unity and I am making endless runner type game, but with end, so it could have levels with different enviroment. Everythink seemed to work ok until I added a trigger that makes player rotate on road turn. Before turn everythink is alright and after it turns, player keeps running forward, but same controls and triggers don't work anymore. I am inserting whole player script, because I don't have any idea why it doesn't work.

public class PlayerControler : MonoBehaviour{

 public bool jump = false;
 public bool slide = false;
 public bool right = false;
 public bool left = false;
 public bool die = false;
 public bool stop = false;
 public bool win = false;
 public Animator anim;
 public Vector3 collidercenter;
 public GameObject player;

 private Touch initialTouch = new Touch();
 private float distance = 0;
 private bool hasSwiped = false;
 private bool isGameStarted;
 public bool death = false;
 public Image gameOverImg;
 public CapsuleCollider myCollider;


 // Start is called before the first frame update
 void Start()
 {
     anim = GetComponent<Animator>();
     myCollider = GetComponent<CapsuleCollider>();
     collidercenter = myCollider.center;
 }

 // Update is called once per frame
 void FixedUpdate()
 {
     foreach (Touch t in Input.touches)
     {
         if (t.phase == TouchPhase.Began)
         {
             initialTouch = t;
         }
         else if (t.phase == TouchPhase.Moved && !hasSwiped)
         {
             float deltaX = initialTouch.position.x - t.position.x;
             float deltaY = initialTouch.position.y - t.position.y;
             distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
             bool swipedSideWay = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);

             if (distance > 100f)
             {
                 if (swipedSideWay && deltaX > 0)
                 {
                     left = true;
                 }
                 if (swipedSideWay && deltaX <= 0)
                 {
                     right = true;
                 }
                 if (!swipedSideWay && deltaY > 0)
                 {
                     slide = true;
                     StartCoroutine(SlideController());
                 }
                 if (!swipedSideWay && deltaY <= 0)
                 {
                     jump = true;
                     StartCoroutine(JumpController());
                 }

                 hasSwiped = true;

             }
         }
         else if (t.phase == TouchPhase.Ended)
         {
             initialTouch = new Touch();
             hasSwiped = false;
         }
     }



     transform.Translate(0, 0, 0.1f);


     if (Input.GetKey(KeyCode.Space) && stop == false && slide == false && right == false && left == false)
     {
         jump = true;
         StartCoroutine(JumpController());
     }

     if (Input.GetKey(KeyCode.DownArrow) && stop == false && jump == false && right == false && left == false)
     {
         slide = true;
         StartCoroutine(SlideController());
     }


     if (Input.GetKey(KeyCode.LeftArrow) && stop == false && right == false && jump == false && slide == false)
     {
         left = true;
         transform.Translate(-0.9f, 0, 0.1f);
     }

     if (Input.GetKey(KeyCode.RightArrow) && stop == false && jump == false && slide == false && left == false)
     {
         right = true;
         transform.Translate(0.9f, 0, 0.1f);
     }

     if ((jump == true) && stop == false && slide == false && right == false && left == false)
     {
         anim.SetBool("Jump", true);
         myCollider.center = myCollider.center + new Vector3(0, 4f, 2f);
         transform.Translate(0, 0.15f, 0.2f);
     }
     else if (jump == false)
     {
         anim.SetBool("Jump", false);
     }

     myCollider.center = collidercenter;

     if ((slide == true) && stop == false && jump == false && right == false && left == false)
     {
         anim.SetBool("Slide", slide);
         transform.Translate(0, 0, 0.1f);
         myCollider.center = Vector3.zero;
         myCollider.height = 2.05f;

     }
     else if (slide == false)
     {
         anim.SetBool("Slide", slide);
         myCollider.height = 6.5f;
     }



 }

 IEnumerator JumpController()
 {
     jump = true;
     yield return new WaitForSeconds(0.5f);
     jump = false;
 }

 IEnumerator SlideController()
 {
     slide = true;
     yield return new WaitForSeconds(0.7f);
     slide = false;
 }
 IEnumerator WinAnim()
 {
     anim.SetBool("win", true);
     win = true;
     yield return new WaitForSeconds(0.5f);
     anim.SetBool("win", false);
     win = false;
 }

 void OnCollisionEnter(Collision other)
 {
     if (other.gameObject.tag == "obstacle")
     {
         Destroy(player, 2.5f);
         anim.SetBool("die", true);
         StartCoroutine(WaitForSceneLoad());
     }

     if (other.gameObject.tag == "WinPoint")
     {
         Destroy(player, 15f);
         StartCoroutine(WinAnim());
         if (win == false)
         {
             anim.SetBool("win02", true);
         }
         StartCoroutine(WaitForSceneToLoad());
     }
 }
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "stop")
     {
         jump = false;
         slide = false;
         left = false;
         right = false;
         stop = true;
     }
   
     if (other.gameObject.tag == "rotate")
     {
         GetComponent<Rigidbody>().angularVelocity = new Vector3(0, -2, 0);
         StartCoroutine(StopRotation());
     }

 } 
 private IEnumerator WaitForSceneToLoad()
 {
     yield return new WaitForSeconds(3);
     SceneManager.LoadScene("WinScene");
 }
 private IEnumerator WaitForSceneLoad ()
 {
     yield return new WaitForSeconds(5);
     SceneManager.LoadScene("GOver");
 }
 private IEnumerator StopRotation()
 {
     yield return new WaitForSeconds(0.8f);
     GetComponent<Rigidbody>().angularVelocity = new Vector3(0, 0, 0);
     GetComponent<Transform>().eulerAngles = new Vector3(0, -90, 0);
 }

}

Do you have any idea what is wrong, why it is not working properly and how to fix it?

Thank you very much

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 xxmariofer · Mar 23, 2020 at 09:54 AM

because when moving the object you are using transform.translate using world axis, what that means is that if you are moving right for example, and you do

 transform.Translate(0, 0.15f, 0.2f);

you are just doing an step forward in that direction. if you have rotated to the right, you should start moving using other axis like

  transform.Translate(0.15f, 0, 0.2f);

i feel it is imposible to implement rotation in your game unless you refactor your whole code, you should be moving using the

 transform.forward * aCostantAmount

hardcoding all the variables will make the code unfixable in the long run

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

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.

Follow this Question

Answers Answers and Comments

125 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

Related Questions

Move player to left and right 1 Answer

Controller axes (specifically the triggers) work in-editor but not in build? 0 Answers

Spawn on trigger 1 Answer

2D endless runner: have the player move in the world instead of moving the world around him 0 Answers

How can I make my car turn? 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