1
votes

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);
    }
2

2 Answers

1
votes

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.

0
votes

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