- Home /
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!
I think i'm just gonna use a character controller
Your answer
Follow this Question
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