- Home /
Locking and Limiting a rotation 2d
Hi!
My game makes the player go up in a rotational arc when a button is pressed, and in a much larger arc downwards when not pressed. What I currently need it way to limit how far these arcs go before they are stopped, or changed into a different type of arc.
More specifically, The downward arc needs to stop entirely when it reaches -90 degrees (or straight down). As for the upward arc, it needs to turn on itself permanently from 90 degrees (or straight up) to 270 degrees (AKA -90), where it automatically turns off the constant upward arc.
Here's my script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float moveSpeed;
public float rotateSpeed;
public float negativeRotateSpeed;
private bool didLower = true;
private Rigidbody2D myRigidbody;
// Use this for initialization
void Start () {
myRigidbody = GetComponent<Rigidbody2D> ();
}
// Used for Graphics and Inputs
void Update () {
transform.position += transform.right * Time.deltaTime * moveSpeed;
if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0) ) {
transform.Rotate(0, 0, Time.deltaTime * rotateSpeed, Space.Self);
didLower = false;
}
if (didLower = true) {
transform.Rotate (0, 0, Time.deltaTime * -rotateSpeed/negativeRotateSpeed, Space.Self);
}
}
// Used for Physics
void FixedUpdate () {
}
}
Thanks in advance!
Your answer
Follow this Question
Related Questions
2D normalise z rotation 1 Answer
When applying AddRelativeForce object reaches higher speed when rotated 1 Answer
Player Rotation to gravity 1 Answer
Movement Relative to Rotated Parent 1 Answer