Camera problems Mouse Scroll Wheel + Padding

Hi,

I’m working on my camera script :

Here it is :

Here is what I’m looking for :

  • If you press the RIGHT mouse button and move the mouse, you zoom on the object. It works :slight_smile:

  • If you press the LEFT mouse button and move the mouse, you turn around the object It works :slight_smile:

  1. If you scroll the mouse wheel you zoom on the object It doesn’t work, can you help me ? [/COLOR]

  2. If you press RIGHT and LEFT button of your mouse, you move the camera in X and Y axes (padding) It works only with the mouse scroll wheel button, but I want to use the RICHT and LEFT button. It doesn’t work, can you help me ?[/COLOR]

  • If you DON’T press RIGHT and LEFT button of your mouse and you move the camera, nothing happend. The camera don’t move. It works :slight_smile:

Does someone can help me to solve my problems ? :face_with_spiral_eyes:

Thanks for your help ! :wink:

Nobody can help me ? :face_with_spiral_eyes:

I really need to solve my problem …

Hi !

I can use the mouse scroll wheel to zoom now : :slight_smile:

Here the code :

Now I need to move (padding) ma camera by pressing the LEFT and RIGHT mouse button … :face_with_spiral_eyes:

Thanks for your help !

Try something like this to make the code a little more straight forward.

enum CameraAction {
    None,
    Zoom,
    Orbit,
    Move,
}

static CameraAction GetCameraAction() {
    bool left = Input.GetMouseButton(0), right = Input.GetMouseButton(1);
    return left ? ( right ? CameraAction.Move : CameraAction.Orbit ) : ( right ? CameraAction.Zoom : CameraAction.None );
}

then change your update a bit:

void Update () 
{	
    CameraAction cameraAction = GetCameraAction();
    if ( !m_CameraTarget ) cameraAction = CameraAction.None;// do nothing without target.

    switch(cameraAction) {
        case CameraAction.Orbit:
            input = true;	
            m_x += Input.GetAxis("Mouse X") * xSpeed; 
            m_y -= Input.GetAxis("Mouse Y") * ySpeed;
            m_y = ClampAngle(m_y, yMinLimit, yMaxLimit);
            Debug.Log("bouton gauche enfonce");
            break;
        case CameraAction.Zoom:
            m_Distance -= Input.GetAxis("Mouse Y") * ySpeed;

            if (m_Distance < minDist) m_Distance = minDist;	
            if (m_Distance > maxDist) m_Distance = maxDist;
            Debug.Log("bouton droit enfonce");
            break;
        case CameraAction.Move:
            {
                Vector3	tmp = m_CameraTarget.position;	
                tmp.y = tmp.y - ( Input.GetAxis("Mouse Y") * ySpeed);
                tmp.x = tmp.x - ( Input.GetAxis("Mouse X") * ySpeed);

                if (tmp.y < m_MinElevation) tmp.y = m_MinElevation;
                if (tmp.y > m_MaxElevation) tmp.y = m_MaxElevation;

                m_CameraTarget.position = tmp;	
                Debug.Log("molette enfoncee");
                break;
       default: break;
    }
    if( cameraAction != CameraAction.None ) UpdateCamera();
}

And that should solve number 4.

unsure about how scroll wheel works unfortunatly.

Thanks for your help but it seems to dosen’t work.

I’m not a programmer, so …

I need to know how I to ma ke an action by pressing the 2 mouse button by script. I don’t know the code to do that.

Can you help me ?

Thanks

Your main problem with your first script you posted is

if(Input.GetMouseButton(0)) { … orbit … }
if(Input.GetMouseButton(1)) { … zoom … }
if(Input.GetMouseButton(2)) { … pan … }
when it should be:

if(Input.GetMouseButton(0) Input.GetMouseButton(1) ) { … pan … }
else if( Input.GetMouseButton(0) ) { … orbit … }
else if( Input.GetMouseButton(1) ) { … zoom … }

Perfect man !! It works many thanks for your help ! :wink:

Here is the final code :

using UnityEngine;
using System.Collections;

public class CameraControl_Basic : MonoBehaviour {

	public	Transform		m_CameraTarget;
	public	float			m_Distance		= 80.0f;
	public	float			xSpeed			=  4.0f;
	public	float			ySpeed			=  4.0f;
	public	int				yMinLimit		= -50;
	public	int				yMaxLimit		=  80;
	public	float			minDist			=  5.0f;
	public	float			maxDist			=  5.0f;
	public	float			m_MinElevation 	= -30.0f;
	public	float			m_MaxElevation 	=  30.0f;
	private	float 			m_x 			= 0.0f;
	private	float		 	m_y 			= 0.0f;
	
	
	// Use this for initialization
	void Start () 
	{
		UpdateCamera();
	}
	
	// Update is called once per frame
	void Update () 
	{	
		if (m_CameraTarget )
		{
			bool input = false;
				
						
			 // Mouse scroll wheel button
			if ( Input.GetMouseButton(2))
			{				
				input = true;
				Vector3	tmp = m_CameraTarget.position;		
				tmp.y = tmp.y - ( Input.GetAxis("Mouse Y") * ySpeed);
		  		tmp.x = tmp.x - ( Input.GetAxis("Mouse X") * ySpeed);
				
				if (tmp.y < m_MinElevation) tmp.y = m_MinElevation;
				if (tmp.y > m_MaxElevation) tmp.y = m_MaxElevation;
		
				m_CameraTarget.position = tmp;	
				Debug.Log("bouton molette enfoncee");
			}	
			
			// Right and Left Button
			if(Input.GetMouseButton(0)  Input.GetMouseButton(1) )
				
			{				
				input = true;
				Vector3	tmp = m_CameraTarget.position;		
				tmp.y = tmp.y - ( Input.GetAxis("Mouse Y") * ySpeed);
				tmp.x = tmp.x - ( Input.GetAxis("Mouse X") * ySpeed);
				
				if (tmp.y < m_MinElevation) tmp.y = m_MinElevation;
				if (tmp.y > m_MaxElevation) tmp.y = m_MaxElevation;
		
				m_CameraTarget.position = tmp;	
				Debug.Log("bouton droit et gauche enfonce");
			}
				
				else if ( Input.GetMouseButton(0) )
			{		
				input = true;					
				m_x += Input.GetAxis("Mouse X") * xSpeed; 
				m_y -= Input.GetAxis("Mouse Y") * ySpeed;
				m_y = ClampAngle(m_y, yMinLimit, yMaxLimit);
				Debug.Log("bouton gauche enfonce");
			}	
				else if ( Input.GetMouseButton(1) )
			{		
				
				input = true;					
				m_Distance -= Input.GetAxis("Mouse Y") * ySpeed;
				
				if (m_Distance < minDist)  m_Distance = minDist;	
				if (m_Distance > maxDist)  m_Distance = maxDist;
				Debug.Log("bouton droit enfonce");
			
			}	
				
			// Mouse scroll wheel 
		   if (Input.GetAxis("Mouse ScrollWheel") != 0){
			    input = true;					
				m_Distance -= Input.GetAxis("Mouse ScrollWheel") * ySpeed;
				
				if (m_Distance < minDist)  m_Distance = minDist;	
				if (m_Distance > maxDist)  m_Distance = maxDist;
				Debug.Log("molette tourne");
		}
			
			if (input) UpdateCamera();
		}
	}
	
	
	public void UpdateCamera ()
	{
		Vector3 	dist = new Vector3(0.0f, 0.0f, -m_Distance);		
		Quaternion 	rotation = Quaternion.Euler(m_y, m_x, 0.0f);	
		Vector3 	position = rotation * dist + m_CameraTarget.position;

		transform.rotation = rotation;
		transform.position = position;	
	}
	
/*
	 void AdjustElevation ()
	{
		Vector3	tmp = m_CameraTarget.position;		
		tmp.y = tmp.y + ( Input.GetAxis("Mouse Y") * ySpeed);

		if (tmp.y < m_MinElevation) tmp.y = m_MinElevation;
		if (tmp.y > m_MaxElevation) tmp.y = m_MaxElevation;
		
		m_CameraTarget.position = tmp;		
	}
	
	
	 void AdjustZoom ()
	{
		m_Distance -= Input.GetAxis("Mouse Y") * ySpeed; 				
		if (m_Distance < minDist)  m_Distance = minDist;	
	}
		*/
	
	float ClampAngle ( float angle, float min, float max)
	{
		if (angle < -360) angle += 360;
		if (angle > 360)  angle -= 360;
		return Mathf.Clamp (angle, min, max);
	}
}