-1
\$\begingroup\$

I have the following script

public class CameraScript : MonoBehaviour {


    public bool AllowZoom;
    public float zoom;
    public float Speed;
    Transform board;
    void Start(){
        board=GameObject.FindGameObjectsWithTag("PlayingBoard")[0].transform;
    }

    void Update(){
       CentreRotate();
    }
    public void arrowMove(){
        if(Input.GetKey(KeyCode.RightArrow)){

            transform.Translate(transform.right*Speed*Time.deltaTime);
        }
        if(Input.GetKey(KeyCode.UpArrow)){
            transform.Translate(transform.up*Speed*Time.deltaTime);
        }
        if(Input.GetKey(KeyCode.LeftArrow)){
            transform.Translate(transform.right*-Speed*Time.deltaTime);
        }
        if(Input.GetKey(KeyCode.DownArrow)){
            transform.Translate(transform.up*-Speed*Time.deltaTime);
        }
    }


    public void CentreRotate(){
        float distance=Mathf.Abs((transform.position-board.position).magnitude);
        float toMove=distance-zoom;
        transform.LookAt(board);
        transform.Translate(transform.forward*toMove);
        arrowMove();
    }
}

This script should allow me to rotate around a given point at a constant distance from the center using the arrow keys. However rather then do that it seems to move towards a spot right above the board and then start turning really fast. It also seems that when slowing down the turning (by dividing distance by a large amount) that the arrow keys give "weird" directions, for example the right arrow key sometimes moves the camera forward.

Anybody got any ideas?

Here are some pictures of what is going on.

On startup:

enter image description here

After I press the right arrow key for a about a second:

enter image description here

\$\endgroup\$
  • \$\begingroup\$ While much of your issue is coming from transform.Translate in your CentreRotate() method, there are all sorts of other problems that you are going to run into without refining your requirements and figuring out what exactly you want, which isn't really mentioned in your question. If you can rephrase your question to be more of a "How do I accomplish X?" providing details of what exactly should happen, maybe even images or a gif or two, that would put you in a much better spot. As it is this is a debug my code question which is off topic. \$\endgroup\$ – SpartanDonut Jun 17 '15 at 18:07
  • \$\begingroup\$ Additionally, I asked and solved a similar problem that might be of use to you. It's not rotating around an object necessary but it results in a very similar effect to what you might be looking for (again, not entirely sure what it is you are looking for). gamedev.stackexchange.com/questions/89693/… \$\endgroup\$ – SpartanDonut Jun 17 '15 at 18:08
0
\$\begingroup\$

You will probably find that what is happening is that the camera object is being rotated by Unity as you translate it. I myself have had this happen where I wanted to rotate around a point, but because the camera was angled down, it would roll as it moved sideways. The best way I found to fix this issue, was to reset the pitch of the camera to look towards the horizon (0), calculate where the camera had to be moved and move it, then pitch the camera down again. You don't have to keep the camera up the whole time the camera's moving though. So: camera x rotation set to 0 translate the camera reset camera rotation

Hopefully, this will fix your problem.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.