Question by
Robsy256 · Oct 14, 2021 at 09:57 PM ·
2drotation2d game2d rotationtransform.rotate
Set Maximum Z Rotation On 2D Player Object?
I need my player object to move to a maximum of z=60, or z=-60 to prevent spinning out, as per the gif below. I've tried a couple of different methods, but I'm obviously missing something with my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float velocity;
private Rigidbody2D rb;
public Transform player;
public float zrotation = 60;
public float rotatespeed = 2;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.W))
{
// Fly Up
//transform.Rotate(Vector3.forward * zrotation * (Time.deltaTime * rotatespeed)); = still spins.
if (transform.rotation.z < zrotation) // This doesn't seem to have any impact...
{
transform.Rotate(new Vector3(0, 0, 1) * zrotation, Space.World); // = still spins
}
rb.velocity = Vector3.up * velocity;
}
if (Input.GetKey(KeyCode.S))
{
// Fly Down
transform.Rotate(Vector3.forward * -zrotation * (Time.deltaTime * rotatespeed));
rb.velocity = Vector3.down * velocity;
}
}
}
Any help would be greatly appreciated!
shipcontrol.gif
(482.9 kB)
Comment