• 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 /
  • Help Room /
avatar image
0
Question by Timon2360 · Jan 24, 2017 at 09:08 PM · cameraquaternionscreenx-axis

Help to edit the C# script. It is necessary to allow camera rotation around its axis with LMB pressed.

What in the script needs fixing so that when you press LMB was the camera rotation?

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

public class MouseLookAlpha : MonoBehaviour {

 public Sprite cursorNormal, cursorMove; // картинки курсора
 public RectTransform _cursor; // UI Image пустой (закрепить в левый нижний угол)
 public float sens = 10; // восприятие курсора к дистанции до края экрана
 public float speed = 3; // скорость передвижения по карте
 public float Xrotation; // Ось Х
 public float Yrotation; // Ось Y
 public float Looksens = 5; // Скорость поворота камеры(вращения)
 public float currXrot; //
 public float currYrot; // 
 public float XrotVel; // Ускорение вращения по Оси Х
 public float YrotVel; // Ускорение вращения оп Оси У
 public float smoothDampT = 0.1f; // замедление перед вращением камеры

 private Image img; // Это курсор
 private Vector3 offset, direction; // 
 private Sprite current; // 

 void Start () // делаем единоразово при старте
 {
     img = _cursor.GetComponent<Image> (); // присваиваем курсору компонент ИМАЭЙДЖ
     Cursor.visible = false; // Видимость курсора выключена
 }

 void Update() // делаем каждый кадр
 {
     if (Input.GetMouseButton (0)) // если нажал и держу ЛКМ
     {
         Xrotation = Input.GetAxis ("Mouse Y") * Looksens; // Поварачиваем по Оси Х * на Скорость поворота камеры
         Yrotation = Input.GetAxis ("Mouse X") * Looksens; // Поварачиваем по Оси У * на Скорость поворота камеры

         currXrot = Mathf.SmoothDamp (currXrot, Xrotation, ref XrotVel, smoothDampT); // Делаем плавность поворота камеры
         currXrot = Mathf.SmoothDamp (currYrot, Yrotation, ref YrotVel, smoothDampT);

         transform.rotation = Quaternion.Euler (Xrotation, Yrotation, 0); // Указываем рабочие Оси вращения
     } 
 }


 void LateUpdate () // делаем в конце каждого кадра
 {
     direction = Vector3.zero;

     if (CheckMouse ()) // проверям состояние мыши
     {
         current = cursorMove; // присваиваем курсору ИМГ двигающегося курсора
     }
     else // иначе
     {
         offset = new Vector3 (_cursor.sizeDelta.x / 2, -_cursor.sizeDelta.y / 2, 0);
         current = cursorNormal; // присваиваем курсору ИМГ стоячего курсора
     }

     _cursor.anchoredPosition = Input.mousePosition + offset;
     float angle = Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg;
     _cursor.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
     direction = Camera.main.transform.TransformDirection (direction);
     direction.y = 0;
     transform.Translate (direction * speed * Time.deltaTime);
     img.sprite = current;
 }

 bool CheckMouse() // Опции проверки состояния курсора
 {
     bool left = false, right = false, down = false, up = false; // Имеющиеся переменные(true/false)

     if (Input.mousePosition.x < sens) // если движение мыши по оси Х дистанции до края экрана
     {
         offset = new Vector3 (_cursor.sizeDelta.x / 2, 0, 0); //двигаем камеру влево
         direction = Vector3.left; // присваиваем курсору направление влево
         left = true; // чекмаус влево
     }
     if (Input.mousePosition.x > Screen.width - sens)
     {
         offset = new Vector3 (-_cursor.sizeDelta.x / 2, 0, 0);
         direction = Vector3.right;
         right = true;
     }
     if (Input.mousePosition.y < sens)
     {
         offset = new Vector3 (0,_cursor.sizeDelta.y / 2, 0);
         direction = Vector3.down;
         down = true;
     }
     if (Input.mousePosition.y > Screen.height - sens)
     {
         offset = new Vector3 (0,-_cursor.sizeDelta.y / 2, 0);
         direction = Vector3.up;
         up = true;
     }
     if (left&&up)
     {
         offset = new Vector3 (_cursor.sizeDelta.x / 2, -_cursor.sizeDelta.y / 2, 0);
         direction = new Vector3(-1,1,0);
         return true;
     }
     else if (left&&down)
     {
         offset = new Vector3 (_cursor.sizeDelta.x / 2, _cursor.sizeDelta.y / 2, 0);
         direction = new Vector3(-1,-1,0);
         return true;
     }
     else if (right&&down)
     {
         offset = new Vector3 (-_cursor.sizeDelta.x / 2, _cursor.sizeDelta.y / 2, 0);
         direction = new Vector3(1,-1,0);
         return true;
     }
     if (right&&up)
     {
         offset = new Vector3 (-_cursor.sizeDelta.x / 2, -_cursor.sizeDelta.y / 2, 0);
         direction = new Vector3(1,1,0);
         return true;
     }
     if (left || up || right || down)
     {
         return true;
     }
     return false;

 }

}

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Screen Space - Camera makes bullets fly in wrong directions 1 Answer

converto text to texture2D 0 Answers

Where is the screen within a camera's frustum? 0 Answers

Frustrating problem with UI canvas (lines of canvas flicker around the edges of screen in game!!!) 5 Answers

UI GameObjects showing in scene view but not game view? 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