- Home /
Rotate Camera on axis and X axis not working
Hey guys. I'm trying to rotate my camera by click and dragging. If I click then drag my mouse horizontally, I want the camera to rotate on the Y axis. If I click and drag vertically, I want my camera to rotate on the x axis. When I let up on the clicking, I want the rotating to stop.
I put this code in a script attached to my camera:
void Update () {
if (pressed) {
if((Input.GetAxis("Horizontal")) == 1){
transform.Rotate(0, Input.GetAxis("Horizontal")*speed*Time.deltaTime, 0,Space.World);
}
else if((Input.GetAxis("Vertical")) == -1){
transform.Rotate(0, Input.GetAxis("Vertical")*speed*Time.deltaTime, 0,Space.World);
}
}
if (Input.GetMouseButtonDown (1)) {
if(pressed) {
pressed = false;
}
else {
pressed = true;
}
}
}
However, when i build and run it, NOTHING happens. It does not rotate in any direction. Can someone help me? What am I doing wrong? Thanks. I'm pretty positive my structure is correct but perhaps my syntax is wrong? please let me know.
Your Get$$anonymous$$ouseButtonDown code is toggling the value pressed when the click occurs, so when you click and release, pressed will be true. The next time you click and release, pressed will be false (well technically, just when you click down). You probably don't want to use Space.World, you need Space.Self. what is speed here? I don't see the variable. Is it large enough? Is this script assigned to your camera?
Speed was a public variable. I actually figured out my problem and I wasn't sensing when Get$$anonymous$$ouseButtonUp was positive. The script was assigned to my camera. It's figured out now though. Thank you! @supernat
Answer by Lastonestanding · May 21 at 01:47 PM
One thing I found out when it comes to changing the angle on the X axis it seems you have to use the transform.eulerAngles to do so. For I am trying to my Camera to Rotate on the X Axis as you zoom in and out here is my code a bit on that:
public class CameraMovement : MonoBehaviour
{
float speed = 0.06f;
float zoomSpeed = 10.0f;
float rotateSpeed = 0.1f;
float maxHeight = 65f;
float minHeight = 3f;
float XposHH = 50f;
float XposHL = 3f;
Vector2 p1;
Vector2 p2;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.LeftShift))
{
speed = 0.06f;
zoomSpeed = 20.0f;
}
else
{
speed = 0.035f;
zoomSpeed = 10.0f;
}
float hsp = transform.position.y * speed * Input.GetAxis("Horizontal");
float vsp = transform.position.y * speed * Input.GetAxis("Vertical");
float scrollSp = -zoomSpeed * Input.GetAxis("Mouse ScrollWheel");
if (((transform.position.y >= maxHeight)) && (scrollSp > 0))
{
scrollSp = 0;
transform.eulerAngles = new Vector3(XposHH, 0, 0);
}
else if ((transform.position.y <= minHeight) && (scrollSp < 0))
{
scrollSp = 0;
transform.eulerAngles = new Vector3(XposHL, 0, 0);
}
But I think this tutorial may be able to help you: