- Home /
Change the z rotation of my player?,how to adjust the z rotation of my player?
Hello, i am very new to unity, programming and C#. I have a small game set up where there is a player object in a river, and rocks spawn above and move straight down. when the player colides with the rocks, they rotate. since im going with a "Canoe" vibe i like this, but i want the canoe to slowly correct itself like its being paddled by a person. i was thinking a statment that checks if the canoe is the default rotation (in this case 180 degrees) and if it isnt issues a rotation force to correct it. any advice is appreciated, thanks!
public float speed = 15.0f;
public float maxVelocity = 4.0f;
private Vector2 screenBounds;
// Start is called before the first frame update
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
}
// Update is called once per frame
void Update()
{
PlayerMoveKeyboard();
//PlayerMoveKeyboardwasd();
if (transform.position.y < -screenBounds.y)
{
Destroy(this.gameObject);
}
//where im trying to get the rotation
if (gameObject.transform.rotation.eulerAngles.z < 180)
{
}
if (gameObject.transform.rotation.eulerAngles.z > 180)
{
}
}
//Moving With Arrow keys
void PlayerMoveKeyboard()
{
//force that pushes the player
float force = 0.0f;
//Max Velocity Of The Player
float velocity = Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x);
//Get Horizonal Velocity
float h = Input.GetAxis("Horizontal");
if (h > 0)
{
if (velocity < maxVelocity)
{
force = speed;
}
}
else if (h < 0)
{
if (velocity < maxVelocity)
{
force = -speed;
}
}
GetComponent<Rigidbody2D>().AddForce(new Vector2(force, 0));
}
//Moving with WASD (Unfinished)
void PlayerMoveKeyboardwasd()
{
float force = 0.0f;
float velocity = Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x);
if (Input.GetKey(KeyCode.A))
{
}
if (Input.GetKey("d"))
{
}
}
Answer by CentauriDev · Apr 26, 2019 at 06:59 AM
You can use transform.Rotate
You can change rotateamount, the higher the faster it goes.
Example:
Transform.Rotate(0,0,rotateamount)
Your answer
Follow this Question
Related Questions
2D Zig-zag movement and rotation 1 Answer
Sprite will not roate when arrow keys are pressed 2 Answers
Turn character the way I am moving 2 Answers
Third Person Character Controller Movement 1 Answer
Rotation used as movement 4 Answers