- Home /
Question by
DavidGaming9999 · Jul 11, 2020 at 08:12 AM ·
rotation2d gamerigidbody2d
,Rotating a 2D sprite with Input
Hello I need help, because im trying to rotate a 2d sprite, but there seems to be some issues. Please help!
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Rigidbody2D shipRigidbody;
const float MoveUnitsPerSecond = 5;
// Start is called before the first frame update
void Start()
{
shipRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float leftInput = Input.GetAxis("Left");
float rightInput = Input.GetAxis("Right");
float verticalInput = Input.GetAxis("Jump");
if(leftInput == 1)
{
transform.Rotate(Vector3.forward * 90);
}
else
{
}
if(rightInput == 1)
{
transform.Rotate(Vector3.forward * -90);
}
else
{
}
if(verticalInput != 0)
{
transform.position += transform.up * Time.deltaTime * MoveUnitsPerSecond;
}
}
}
,Hello. So, I've got a messy problem. I'm trying to rotate a 2D sprite to make a "Sky Force" based game. But I have the problem of rotating the 2D sprite I'm going to use. I don't know if its something of my code, but here it is just in case.
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
Rigidbody2D shipRigidbody;
const float MoveUnitsPerSecond = 5;
// Start is called before the first frame update
void Start()
{
shipRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float leftInput = Input.GetAxis("Left");
float rightInput = Input.GetAxis("Right");
float verticalInput = Input.GetAxis("Jump");
if(leftInput == 1)
{
transform.Rotate(Vector3.forward * 90);
}
else
{
}
if(rightInput == 1)
{
transform.Rotate(Vector3.forward * -90);
}
else
{
}
if(verticalInput != 0)
{
transform.position += transform.up * Time.deltaTime * MoveUnitsPerSecond;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Strange Problem with Torque-based Rotation 0 Answers
Rigidbody2D keeps rotating when freeze position is checked 1 Answer
Rigidbody2d physic movement on 2d terrain. 0 Answers
Drag to add power, release to throw at opposite angle (RigidBody2D), strange beahviour. 3 Answers
Rotate within range 1 Answer