Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Psyche · Apr 11, 2011 at 02:42 PM · mouselook

Create script " SmoothMouseLookClick"

Hello everybody!

First of all, I don't know anything about Scripting, I only use part of script find here and there.

I'm just trying to create a script to look only when the left button of the mouse is pressed. And also try to reduce the "micro freeze" on the mouse move.

For now I succeed in moving the head on axis X and Y when I press the mouse left button. But the issue is that my Y axis is always "activated" and I can move along this axis without pressing the mouse button.

If anyone can help me on this, it would be great.

here is my code:

using UnityEngine;

using System.Collections; using System.Collections.Generic;

[AddComponentMenu("Camera-Control/Smooth Mouse Look")] public class SmoothMouseLookClick : MonoBehaviour {

public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 } public RotationAxes axes = RotationAxes.MouseXAndY; public float sensitivityX = 15F; public float sensitivityY = 15F;

public float minimumX = -360F; public float maximumX = 360F;

public float minimumY = -60F; public float maximumY = 60F;

float rotationX = 0F; float rotationY = 0F;

private List<float> rotArrayX = new List<float>(); float rotAverageX = 0F;

private List<float> rotArrayY = new List<float>(); float rotAverageY = 0F;

public float frameCounter = 20;

Quaternion originalRotation;

void Update () { if (Input.GetMouseButton(0)) if (axes == RotationAxes.MouseXAndY) {
rotAverageY = 0f; rotAverageX = 0f;

     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
     rotationX += Input.GetAxis("Mouse X") * sensitivityX;

     rotArrayY.Add(rotationY);
     rotArrayX.Add(rotationX);

     if (rotArrayY.Count &gt;= frameCounter) {
         rotArrayY.RemoveAt(0);
     }
     if (rotArrayX.Count &gt;= frameCounter) {
         rotArrayX.RemoveAt(0);
     }

     for(int j = 0; j &lt; rotArrayY.Count; j++) {
         rotAverageY += rotArrayY[j];
     }
     for(int i = 0; i &lt; rotArrayX.Count; i++) {
         rotAverageX += rotArrayX[i];
     }

     rotAverageY /= rotArrayY.Count;
     rotAverageX /= rotArrayX.Count;

     rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);
     rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);

     Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
     Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);

     transform.localRotation = originalRotation * xQuaternion * yQuaternion;
 }
 else if (axes == RotationAxes.MouseX)
 {           
     rotAverageX = 0f;

     rotationX += Input.GetAxis("Mouse X") * sensitivityX;

     rotArrayX.Add(rotationX);

     if (rotArrayX.Count &gt;= frameCounter) {
         rotArrayX.RemoveAt(0);
     }
     for(int i = 0; i &lt; rotArrayX.Count; i++) {
         rotAverageX += rotArrayX[i];
     }
     rotAverageX /= rotArrayX.Count;

     rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);

     Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
     transform.localRotation = originalRotation * xQuaternion;           
 }
 else
 {           
     rotAverageY = 0f;

     rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

     rotArrayY.Add(rotationY);

     if (rotArrayY.Count &gt;= frameCounter) {
         rotArrayY.RemoveAt(0);
     }
     for(int j = 0; j &lt; rotArrayY.Count; j++) {
         rotAverageY += rotArrayY[j];
     }
     rotAverageY /= rotArrayY.Count;

     rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);

     Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
     transform.localRotation = originalRotation * yQuaternion;
 }

}

void Start () {
if (rigidbody) rigidbody.freezeRotation = true; originalRotation = transform.localRotation; }

public static float ClampAngle (float angle, float min, float max) { angle = angle % 360; if ((angle >= -360F) && (angle <= 360F)) { if (angle < -360F) { angle += 360F; } if (angle > 360F) { angle -= 360F; }
} return Mathf.Clamp (angle, min, max); }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Byterunner · Apr 11, 2011 at 03:48 PM

You want to have { } around everything after if (Input.GetMouseButton(0)) to the end of that function. Otherwise, the else statements are being attached to the first if, not the second. Like so:

using UnityEngine; using System.Collections; using System.Collections.Generic;

[AddComponentMenu("Camera-Control/Smooth Mouse Look")]

public class SmoothMouseLookClick : MonoBehaviour {

 public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
 public RotationAxes axes = RotationAxes.MouseXAndY;
 public float sensitivityX = 15F;
 public float sensitivityY = 15F;

 public float minimumX = -360F;
 public float maximumX = 360F;

 public float minimumY = -60F;
 public float maximumY = 60F;

 float rotationX = 0F;
 float rotationY = 0F;

 private List&lt;float&gt; rotArrayX = new List&lt;float&gt;();
 float rotAverageX = 0F; 

 private List&lt;float&gt; rotArrayY = new List&lt;float&gt;();
 float rotAverageY = 0F;

 public float frameCounter = 20;

 Quaternion originalRotation;

 void Update ()
 {
     if (Input.GetMouseButton(0))
     {
         if (axes == RotationAxes.MouseXAndY)
         {           
             rotAverageY = 0f;
             rotAverageX = 0f;

             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationX += Input.GetAxis("Mouse X") * sensitivityX;

             rotArrayY.Add(rotationY);
             rotArrayX.Add(rotationX);

             if (rotArrayY.Count &gt;= frameCounter) {
                 rotArrayY.RemoveAt(0);
             }
             if (rotArrayX.Count &gt;= frameCounter) {
                 rotArrayX.RemoveAt(0);
             }

             for(int j = 0; j &lt; rotArrayY.Count; j++) {
                 rotAverageY += rotArrayY[j];
             }
             for(int i = 0; i &lt; rotArrayX.Count; i++) {
                 rotAverageX += rotArrayX[i];
             }

             rotAverageY /= rotArrayY.Count;
             rotAverageX /= rotArrayX.Count;

             rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);
             rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);

             Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
             Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);

             transform.localRotation = originalRotation * xQuaternion * yQuaternion;
         }
         else if (axes == RotationAxes.MouseX)
         {           
             rotAverageX = 0f;

             rotationX += Input.GetAxis("Mouse X") * sensitivityX;

             rotArrayX.Add(rotationX);

             if (rotArrayX.Count &gt;= frameCounter) {
                 rotArrayX.RemoveAt(0);
             }
             for(int i = 0; i &lt; rotArrayX.Count; i++) {
                 rotAverageX += rotArrayX[i];
             }
             rotAverageX /= rotArrayX.Count;

             rotAverageX = ClampAngle (rotAverageX, minimumX, maximumX);

             Quaternion xQuaternion = Quaternion.AngleAxis (rotAverageX, Vector3.up);
             transform.localRotation = originalRotation * xQuaternion;           
         }
         else
         {           
             rotAverageY = 0f;

             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;

             rotArrayY.Add(rotationY);

             if (rotArrayY.Count &gt;= frameCounter) {
                 rotArrayY.RemoveAt(0);
             }
             for(int j = 0; j &lt; rotArrayY.Count; j++) {
                 rotAverageY += rotArrayY[j];
             }
             rotAverageY /= rotArrayY.Count;

             rotAverageY = ClampAngle (rotAverageY, minimumY, maximumY);

             Quaternion yQuaternion = Quaternion.AngleAxis (rotAverageY, Vector3.left);
             transform.localRotation = originalRotation * yQuaternion;
         }
     }
 }

 void Start ()
 {           
     if (rigidbody)
         rigidbody.freezeRotation = true;
     originalRotation = transform.localRotation;
 }

 public static float ClampAngle (float angle, float min, float max)
 {
     angle = angle % 360;
     if ((angle &gt;= -360F) &amp;&amp; (angle &lt;= 360F)) 
     {
         if (angle &lt; -360F) {
             angle += 360F;
         }
         if (angle &gt; 360F) {
             angle -= 360F;
         }           
     }
     return Mathf.Clamp(angle, min, max);
 }

}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Psyche · Apr 11, 2011 at 04:39 PM 0
Share

thanks for the tips, however I cannot switch off the "Auto move"( move onto the Y axis without press the left mouse button) on the Y axis.

avatar image Byterunner · Apr 15, 2011 at 04:48 PM 0
Share

I can't decipher what behavior you are attempting to achieve; your sentence doesn't make any sense to me. If you want a script that moves the camera only while holding down the left mouse button, what I've posted works (for both X and Y axis). If you want to achieve something different, please give more details about what you want.

avatar image wuen18 · Oct 02, 2012 at 10:46 AM 0
Share

Hi all. I'm trying to use this code applied at ($$anonymous$$ouse Look) $$anonymous$$ain Camera within Frist Person Controler, but does not work. I changed the script in C + default and moves back and forth but the nod pressing the mouse button effect does not arise. Help, please.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

How can I pause the "Mouse Look" script when I pause my game? 2 Answers

Script to disable MouseLook? 2 Answers

MouseLook not being disabled when attached to a camera 0 Answers

Error BCE0051: Operater '==' cannot be used. Please Help! 1 Answer

Mouse Look script for split screen game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges