1

Have this camera working perfect on my target object with one caveat. Can't seem to get the character to look up and down. Left and right move perfectly, up and down don't move at all. What am I doing wrong with the "Mouse Y" part?

    public GameObject target;
    public float rotateSpeed = 7;
    Vector3 offset;

    void Start() {
        offset = target.transform.position - transform.position;
    }

    void LateUpdate() {
        float horizontal = Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime;
        float verticle = Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime;
        target.transform.Rotate(0, horizontal, 0);

        float desiredAngle = target.transform.eulerAngles.y;

        Quaternion rotation = Quaternion.Euler(0, desiredAngle, verticle);
        transform.position = target.transform.position - (rotation * offset);

        transform.LookAt(target.transform);
    }
Andrew
  • 4,953
  • 15
  • 40
  • 58
Volearix
  • 1,573
  • 3
  • 23
  • 49

2 Answers2

2

You're not using verticle in your Transform.Rotate call (vertical?). Edit: Sorry, I just stopped looking once I got to the first problem, looking further there's another problem that's similar to the one I addressed in this question. The "order of operations" is wrong (see the new comments):

public GameObject target;
public float rotateSpeed = 7;
Vector3 offset;

void Start() {
    offset = target.transform.position - transform.position;
}

void LateUpdate() {
    float horizontal = Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime;
    float verticle = Input.GetAxis("Mouse Y") * rotateSpeed * Time.deltaTime;
    //You didn't use verticle before. Depending on your model/setup you might verticle as the 3rd (Z) parameter to get rotation in the right direction
    target.transform.Rotate(verticle, horizontal, 0);  //This line rotates the transform

    float desiredAngle = target.transform.eulerAngles.y;

    Quaternion rotation = Quaternion.Euler(0, desiredAngle, verticle);
    transform.position = target.transform.position - (rotation * offset);

    transform.LookAt(target.transform); //This sets the absolute rotation of the transform. This call will "override" the above call to Rotate
}

To give more information you'll have to give an explanation of what your end goal is with this code, because looking closer I'm seeing "oddities" with what the code sample is trying to do.

Community
  • 1
  • 1
Selali Adobor
  • 2,060
  • 18
  • 30
  • Tried this before. Seems to want to move but doesn't. The screen gets shaky like it's trying to move up or down but then refocuses on the player. – Volearix Oct 02 '14 at 20:50
  • And actually this particular code just makes my player spin on the x axis and the camera doesn't follow... :\ – Volearix Oct 02 '14 at 20:53
  • I added some comments to explain why it doesn't move, but you'll need to add some information to the question. – Selali Adobor Oct 03 '14 at 01:32
0

The last line of code (transform.Lookat) overrides the previous code ... basically says 'always look at the target no matter what else happens'.

Bowb
  • 36
  • 4