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 Zedock · Oct 20, 2015 at 12:03 PM · rotationphysicsaxisclonez-axis

Rotation eulerangles not adjusting correctly...

I am trying to make a simple flappy bird clone (in order to act as a sort of practise / first game, NOTE: This is not my first game). I am trying to imitate the rotation that occurs whenever you "fly" (as in pointing up, then gradually point downwards). I have tried using smoothdamp and lerp but neither are working... The outcome of what the code is outputting, is that the bird just keeps rotation (z axis) with no stop.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
     
 public class Fly : MonoBehaviour 
 {    
 public float flyVelocity;
 Rigidbody2D rb;
 bool canFly;
 Vector3 currentRotation;
     public Vector3 targetRotation;
     float rotationSpeed = 180f;
     float smoothTime = 0.3f;
     //Vector3 rotationVel = new Vector3 (0, flyVelocity, 0);
 
 void Update()
     {
         currentRotation = transform.eulerAngles;
         currentRotation.z -= 10f;
         
         if(transform.eulerAngles.z <=-40)
         {
             currentRotation.z = -40;
         }
         transform.eulerAngles = currentRotation;
         
         if(Input.GetButtonDown("Jump") && canFly)
         {
             currentRotation = new Vector3(
             0,
             0,
             Mathf.LerpAngle(currentRotation.z, targetRotation.z, Time.deltaTime));
             
             transform.eulerAngles = currentRotation;
             rb.velocity = new Vector2(rb.velocity.x, flyVelocity);
             
         }
 }
 }
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

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by toromano · Oct 21, 2015 at 08:50 AM

My suggestion would be something like this for rotation since rotation is related velocity in y diretion:

     Quaternion upRotation;
     Quaternion downRotation;
     void Start () {
         float upAngle = -40.0f; //modify this angle
         float downAngle = 40.0f; //modify this angle
         upRotation = Quaternion.EulerAngles( new Vector3(0,0,upAngle));
         downRotation = Quaternion.EulerAngles( new Vector3(0,0,downAngle));
     }
     
     // Update is called once per frame
  void Update()
   {
      if(rb.velocity.y < .0f) // when falling
      {
          transform.rotation = Quaternion.Lerp(transform.rotation,downRotation, Time.deltaTime * 5.0f);
      }
      else
      {
          transform.rotation = Quaternion.Lerp(transform.rotation,upRotation, Time.deltaTime * 5.0f);
      }
 }
Comment
Add comment · Show 2 · 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 Zedock · Oct 22, 2015 at 08:14 AM 0
Share

$$anonymous$$ate, you're a bloody legend. Works perfectly... Exactly what I needed :)... Except for the fact that the character is upside-down. Just for future reference (if anyone were to see this page in the future), if you don't want the character to be upside-down, set the upAngle to -100, and the downAngle to 100. It worked for me.

NOTE: Rotations may vary, my rotations were 0,0,0.

avatar image toromano Zedock · Oct 22, 2015 at 08:25 AM 0
Share

Ahhah Thanks! :)

avatar image
0

Answer by PictonicStudio · Oct 20, 2015 at 05:17 PM

Try this method instead.

     public float jumpforce = 100;
     public Rigidbody2D mainRigidbody;
     public bool grounded;
 
     private float tempAngle;
 
     private void Start()
     {
         mainRigidbody = GetComponent<Rigidbody2D>();
     }
 
     private void Update()
     {
         RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x,transform.position.y-0.42f), -Vector2.up);
         //Change 0.46f to half of the 2d collide height.
         grounded = hit.distance <= 0.46f ? true : false;
   
         Debug.Log(hit.distance);
         if (Input.GetKeyDown(KeyCode.Space) && grounded)
         {
             mainRigidbody.AddForce(Vector3.up * jumpforce);
         }
 
         if (mainRigidbody.velocity.y < 0)
         {
             tempAngle = -hit.distance * (jumpforce / 45);
         }
         else
         {
             tempAngle = 0;
         }
 
         transform.eulerAngles = new Vector3(0,0,Mathf.LerpAngle(transform.eulerAngles.z,tempAngle,Time.deltaTime*6));
     }
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 Zedock · Oct 21, 2015 at 08:31 AM 0
Share

Sorry but this isn't working, it seems to be a replacement jump script... I am looking for a fix for my rotation script...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

make object rotate towards its direction after bounce 1 Answer

Equivalent to this using torque 1 Answer

Rotating projectile 2 Answers

Camera rotating with physics based movement. 0 Answers

Mathf.Clamp acts strange when going below 0 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