1

I'm currently trying to create a Camera Control for unity (to follow around a prefab and be able to zoom and such ...

I am very new to C#

an issue I am having with this script is that

  • camera zooming to 0,0,0. (when I need it to stay at it's current Y-axis, I tried changing the void "Move()" but the vector requires 3 m_....'s

I also need to write a piece of code that will allow the player to zoom the camera in and out using the scroll wheel... (In "Public Void Update()"...

I've been looking through guides and videos and can't find anything to assist me with this..

this is the section of code I require help with :

private void FixedUpdate()
{
    Move();
}

private void Move()
{
    m_DesiredPosition = m_target.position;
    transform.position = Vector3.SmoothDamp(transform.position,
        m_DesiredPosition, ref m_MoveVelocity, m_DampTime);
}

public void Update()
{
    // Get the scroll value of the mouse scroll wheel
    // float scroll = Input.GetAxis("Mouse ScrollWheel");

    // Is the scroll value not 0?

    // Modify the orthographic size by the scroll value
    Camera.main.orthographicSize = 4.8f;
}

1 Answer 1

4

For keeping the camera at Y = 0 simply override Y:

m_DesiredPosition = m_Target.position;
m_DesiredPosition.Y = 0;
transform.position = Vector3.SmoothDamp(transform.position,
    m_DesiredPosition, ref m_MoveVelocity, m_DampTime);

For zooming the camera you'll want to add/subtract the value to orthographicsize instead of simply setting it:

// Zoom in
Camera.main.orthographicSize -= 4.8f;
// Zoom out
Camera.main.orthographicSize += 4.8f;
Sign up to request clarification or add additional context in comments.

2 Comments

cheers, how would I implement the zoom in and out code to happen when i scroll the mouse wheel ... and do happen to know why this issue is happening ... i.imgur.com/Kwb2GhQ.png
The formatting will look bad here, but you're going to want to check the input of the mouse scroll wheel: if (Input.GetAxis("Mouse ScrollWheel") < 0) { // zoom out Camera.main.orthographicSize += 4.8f; } if (Input.GetAxis("Mouse ScrollWheel") > 0) { // zoom in Camera.main.orthographicSize -= 4.8f; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.