Blatchford
Blatchford

Reputation: 13

Unity C# camera movement

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

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

Upvotes: 1

Views: 645

Answers (1)

Hrethric
Hrethric

Reputation: 96

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;

Upvotes: 4

Related Questions