- Home /
Camera rotation stop if x is 50
Hello. I am creating a game and i want to stop the camera rotation to x more if reached 50 x. I had this in testing game that is working:
void Update () {
if(Input.GetAxis ("Mouse ScrollWheel") > 0f )
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
if(Input.GetAxis ("Mouse ScrollWheel") < 0f )
transform.Translate (Vector3.back * moveSpeed * Time.deltaTime);
if (transform.position.y > 14)
transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
if (transform.position.y < 2)
transform.Translate (Vector3.back * moveSpeed * Time.deltaTime);
The code is checking y position not rotation if the height is more than 14 the camera stopped did not move more higher same with the bottom if reached height 2 camera stopped did not go more than 2 down. But now with rotation i changed the same code to check for rotation x the same way but it does not work but it should. Here is my rotation check:
using UnityEngine;
using System.Collections;
public class CameraControl : MonoBehaviour {
public float cameraspeed = 30f;
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.UpArrow))
transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
if(Input.GetKey(KeyCode.DownArrow))
transform.Rotate (Vector3.right * cameraspeed * Time.deltaTime);
if (transform.rotation.x > 50)
transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
}
}
Soo i hope someone can find the problem and tell me how to fix this rotation x checker. Thanks.
Answer by Addyarb · May 03, 2015 at 06:01 PM
Change line 17 to:
if (transform.rotation.x > .4229)
Unity internally measures angles in quaternion angles, which are way different than the angles you see in the transform/inspector (known as Euler angles).
Alternatively, you can convert them for your convenience.
Like so:
float angle = transform.rotation.eulerAngles.x; // define our angle up here..
if(Input.GetKey(KeyCode.UpArrow))
transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
if (Input.GetKey (KeyCode.DownArrow)) {
transform.Rotate (Vector3.right * cameraspeed * Time.deltaTime);
}
if (angle > 50)
transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
}
I have a error using the float angle. This is my updated code:
using UnityEngine;
using System.Collections;
public class CameraControl : $$anonymous$$onoBehaviour {
public float cameraspeed = 30f;
float angle = transform.rotation.eulerAngles.x;
// Update is called once per frame
void Update () {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow))
transform.Rotate (Vector3.right * cameraspeed * Time.deltaTime);
if (angle > 50)
transform.Rotate (Vector3.left * cameraspeed * Time.deltaTime);
}
}
And here is the error: Assets/Cookie Clicker/Scripts/Camera/CameraControl.cs(8,26): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `UnityEngine.Component.transform' How do i fix it?
I tried if (transform.rotation.x > .4229) i still can rotate left more than 50. If you need i can take a video or a screenshot
On line 17 try this: if(transform.eulerAngles.x > 50) { //Rest of the code }
Hey if you can read this comment maybe you know how to fix if i move the camera up if it reach 0 it goes down from 380 how do i make it dont do that and if i reach 0 from going up go to -1... ?
Answer by Zinatulin · May 04, 2015 at 12:12 PM
I think you should try this on line 17:
if(transform.eulerAngles.x > 50)
{
transform.rotate(Vector3.left * cameraspeed * Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
Know if an object has been rotating 180 or 360 degrees? 0 Answers
Multiple Cars not working 1 Answer
Grid-based game movement 1 Answer