Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 FlorianBaptiste · May 29, 2015 at 05:38 AM · rotationmovementrigidbodyrigidbody2d

Help with translation and rotation on a rigid body 2D

Hello everybody,

I know this is a problem that has already been mentioned several times on UA but I have already spent a lot of time looking for a solution and I really can't find it. For information, i am a 100% beginner in code and in Unity.

Basically, my game is a 2D platformer with horizontal scroll (player is a cube). Movements are quite simple:

  • Player can fly (he's going up)

  • Player can "drop" (going down)

  • Player moves forward automatically

I added a rigid body on the player and I'm using gravity to go down. The rest is in the code below. I removed some parts that are not relevant for this but i can put the whole code if needed.

Everything was working fine until I tried to add some rotation to the player in order to give a real effect of flying when he's going up or of falling when he's going down. Cube seems to slow down a lot when rotating or after having rotated and it goes off the camera field. I tried Slerp, transform.Rotate(), AddTorque but I still have the same issue.

Here is the code for the player.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class PlayerScript : MonoBehaviour
 {
     public GameControlScript control;                        // Reference to GameControlScript to get game speed
 
     // variables for player movements
     float speed;                                            // Game speed
     Rigidbody2D rigidBody;
     public FlyScript flyScript;
     float maxY;
 
     bool isMovingUp;
     bool isMovingDown;
     Vector3 LastPOS;
     Vector3 NextPOS;
 
 
 
     void Start()
     {
 
         rigidBody = GetComponent<Rigidbody2D>();
 
     } 
     
 
     void FixedUpdate()
     {
 
         // Player translates automatically in function of game's speed
             //` -> game speed is a float set in another script
         float speed = control.gameSpeed;
         transform.Translate(new Vector2(1, 0) * speed * Time.deltaTime);

 
         // if player press fly button, then we change rigidbody velocity and we decrease gravity effect
         if(flyScript.CanFly() )
         {
             rigidBody.velocity = new Vector2(0, 5);
             //Vector2 force = new Vector2(0, 5);
             //rigidBody.AddForce(force);
             rigidBody.gravityScale = 1;
         }
         // gravity is increased when user is not pressing fly button
         else
             rigidBody.gravityScale = 2;
     }
     
     // Part used for rotation. Not finished
     void LateUpdate()
     {
 
         
         NextPOS.y = transform.position.y;
 
         // if player is going up
         if(LastPOS.y < NextPOS.y)
         {
             isMovingUp = true;
             isMovingDown = false;
         }

             // if player is going down
         if(LastPOS.y > NextPOS.y)
         {
             isMovingDown = true;
             isMovingUp = false;
             //transform.rotation = Quaternion.Euler(0,0,45);
         }

             // if player is steady
         else if(LastPOS.y == NextPOS.y)
         {
             isMovingUp = false;
             isMovingDown = false;
         }
         
         LastPOS.y = NextPOS.y;
     }
     }


And here is the code for the camera

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour
 {
     // Reference to GameControlScript to get game speed
     public GameControlScript control;                            
 
     void Update ()
     {
         // Camera translates automatically in function of game speed
         float speed = control.gameSpeed;
         transform.Translate(Vector2.right * speed * Time.deltaTime);
     }
     } 

Maybe the problem doesn't come from the slerp or torque but from somewhere else in the code. I honestly don't know but if someone could help me out, that would be great.

Thanks!

Comment
Add comment · Show 1
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 FlorianBaptiste · May 31, 2015 at 11:46 AM 0
Share

I think i'm just gonna use a character controller

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

How to make a RigidBody not go into ground when tilted foward? 2 Answers

InvalidCastException: Cannot convert from source type to destination type using a Rigidbody2D 2 Answers

Player movement in the direction of rotation INACCURATE? 3 Answers

Rotate object towards target without influencing AddForce 0 Answers

Change movement speed based on rotation 0 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