Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 aubenedis · Apr 15, 2020 at 11:06 AM · camerarotationobjectaround

Help, please with rotation input

I have this code, which is work on desktop webGL perfectly, but when I open page with web GL three mobile browser input is not working correctly. when touch screen, my target object is start to move without drag... Could somebody please help me

using UnityEngine;

public class CameraController : MonoBehaviour { public GameObject heads; Camera cam; public bool isControlable; private Vector3 screenPoint; private Vector3 offset; public GameObject target; public float distance = 1200f; public float xSpeed = 10f; public float ySpeed = 10f;

 public float yMinLimit = -80f;
 public float yMaxLimit = 80f;
 public float yMinLimit1 = -60f;
 public float yMaxLimit1 = 60f;

 public float distanceMin = 600f;
 public float distanceMax = 1200f;

 public float smoothTime = 2f;

 public float rotationYAxis = 0.0f;
 float rotationXAxis = 0.0f;

 float velocityX = 0.0f;
 float velocityY = 0.0f;
 float moveDirection = -1;




if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)

      public void SetControllable(bool value)
 {
     isControlable = value;
 }

 // Use this for initialization
 void Start()
 {
     cam = GetComponentInChildren<Camera>();
     Vector3 angles = transform.eulerAngles;
     rotationYAxis = (rotationYAxis == 0) ? angles.y : rotationYAxis;
     rotationXAxis = angles.x;

     Rigidbody rigidbody = GetComponent<Rigidbody>();

     // Make the rigid body not change rotation
     if (rigidbody)
     {
         rigidbody.freezeRotation = true;
     }
 }


  



 void LateUpdate()
 {

if (target) { if (Input.touches[0] && isControlable) { velocityX = Input.touches[0].deltaPosition.x touch_sens; velocityY = Input.touches[0].deltaPosition.y touch_sens; }

         if (isControlable)
         {
             if (heads.activeSelf)
             {
               
                 distance = distanceMax;
                 target.transform.position = new Vector3(-0.5444031f, -176f, -11f);

                 rotationYAxis += velocityX;
                 rotationXAxis -= velocityY;

                 rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);

                 Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
                 Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
                 Quaternion rotation = toRotation;

                 Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
                 Vector3 position = rotation * negDistance + target.transform.position;

                 transform.rotation = rotation;
                 transform.position = position;

                 velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
                 velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);

                 //screenPoint = cam.WorldToScreenPoint(target.transform.position);
                 //offset = target.transform.position - cam.ScreenToWorldPoint(new Vector3(moveDirection * Input.mousePosition.x, moveDirection * Input.mousePosition.y, screenPoint.z));
             }
             if (!heads.activeSelf)
             {
          
                 distance = distanceMin;
                 target.transform.position = new Vector3(-0.5444031f, 308f, -11f);

                 rotationYAxis += velocityX;
                 rotationXAxis -= velocityY;

                 rotationXAxis = ClampAngle(rotationXAxis, yMinLimit1, yMaxLimit1);

                 Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
                 Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
                 Quaternion rotation = toRotation;

                 Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
                 Vector3 position = rotation * negDistance + target.transform.position;

                 transform.rotation = rotation;
                 transform.position = position;

                 velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
                 velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);

                 //screenPoint = cam.WorldToScreenPoint(target.transform.position);
                 //offset = target.transform.position - cam.ScreenToWorldPoint(new Vector3(moveDirection * Input.mousePosition.x, moveDirection * Input.mousePosition.y, screenPoint.z));
             }
         }

        
     }

 }

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







else

    void Start()
     {
         cam = GetComponentInChildren<Camera>();
         Vector3 angles = transform.eulerAngles;
         rotationYAxis = (rotationYAxis == 0) ? angles.y : rotationYAxis;
         rotationXAxis = angles.x;

         Rigidbody rigidbody = GetComponent<Rigidbody>();

         // Make the rigid body not change rotation
         if (rigidbody)
         {
             rigidbody.freezeRotation = true;
         }
     }

     void LateUpdate()
     {

         if (target)
     {
         if (Input.GetMouseButton(0) && isControlable)
         {
             velocityX = xSpeed * Input.GetAxis("Mouse X");
             velocityY = ySpeed * Input.GetAxis("Mouse Y");
         }


         if (isControlable)
         {
             if (heads.activeSelf)
             {
               
                 distance = distanceMax;
                 target.transform.position = new Vector3(-0.5444031f, -176f, -11f);

                 rotationYAxis += velocityX;
                 rotationXAxis -= velocityY;

                 rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);

                 Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
                 Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
                 Quaternion rotation = toRotation;

                 Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
                 Vector3 position = rotation * negDistance + target.transform.position;

                 transform.rotation = rotation;
                 transform.position = position;

                 velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
                 velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);

                 //screenPoint = cam.WorldToScreenPoint(target.transform.position);
                 //offset = target.transform.position - cam.ScreenToWorldPoint(new Vector3(moveDirection * Input.mousePosition.x, moveDirection * Input.mousePosition.y, screenPoint.z));
             }
             if (!heads.activeSelf)
             {
          
                 distance = distanceMin;
                 target.transform.position = new Vector3(-0.5444031f, 308f, -11f);

                 rotationYAxis += velocityX;
                 rotationXAxis -= velocityY;

                 rotationXAxis = ClampAngle(rotationXAxis, yMinLimit1, yMaxLimit1);

                 Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
                 Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
                 Quaternion rotation = toRotation;

                 Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
                 Vector3 position = rotation * negDistance + target.transform.position;

                 transform.rotation = rotation;
                 transform.position = position;

                 velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
                 velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);

                 //screenPoint = cam.WorldToScreenPoint(target.transform.position);
                 //offset = target.transform.position - cam.ScreenToWorldPoint(new Vector3(moveDirection * Input.mousePosition.x, moveDirection * Input.mousePosition.y, screenPoint.z));
             }
         }

        
     }

 }

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

} #endif

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.

Follow this Question

Answers Answers and Comments

214 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 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

Rotate a moving object 0 Answers

How to Rotate Camera around GameObject (Player) 0 Answers

Object Rotation Relative to Camera 1 Answer

Rotate object ON THE CAMERA UP AXIS 2 Answers

Make object appear within camera 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