• 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 wildlo93 · Jul 20, 2016 at 09:03 PM · touchtouchscreentouchphasefootball

made a football game and my touches works fine on my laptop but on my device only the upper region works

i have made a football game also worke with physics it is working fine but the problem on my device the touch is responsive only in the upper corner may it be potraitor may it be horizontal i tried to change factors but it didnt work i dont know what is the problem ..... following is the code ............. please help me out here ............Bear with me people i know the code too long but help me out here ............

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 using Random = UnityEngine.Random;
 
 
 public class Shoot : MonoBehaviour
 {
 
     public static Shoot share;
 
     public static Action EventShoot = delegate { };
     public static Action<float> EventChangeSpeedZ = delegate { };
     public static Action<float> EventChangeBallZ = delegate { };
     public static Action<float> EventChangeBallX = delegate { };
     public static Action<float> EventChangeBallLimit = delegate { };
     public static Action<Collision> EventOnCollisionEnter = delegate { };
     public static Action EventDidPrepareNewTurn = delegate { };
 
 
     public float _ballControlLimit;
 
     public Transform _goalKeeper;
     public Transform _ballTarget;
     protected Vector3 beginPos;
     protected bool _isShoot = false;
 
     public float minDistance = 40f;     // 40f
 
 
     public Rigidbody _ball;
     public float factorUp = 10f;             // 10f
     public float factorDown = 1f;           // 1f
     public float factorLeftRight = 2f;        // 2f
     public float factorLeftRightMultiply = 2f;        // 2f
     public float _zVelocity = 24f;
 
     public AnimationCurve _curve;
     protected Camera _mainCam;
 
     protected float factorUpConstant = 0.015f * 960f;   // 0.015f * 960f;
     protected float factorDownConstant = 0.005f * 960f; // 0.005f * 960f;
     protected float factorLeftRightConstant = 0.03f * 640f; // 0.03f * 640f; // 0.03f * 640f;
 
     public Transform _ballShadow;
 
 
     public float _speedMin = 20f;   // 20f;
     public float _speedMax = 36f;   // 36f;
 
     public float _distanceMinZ = 16.5f;
     public float _distanceMaxZ = 35f;
 
     public float _distanceMinX = -25f;
     public float _distanceMaxX = 25f;
 
     public bool _isShooting = false;
     public bool _canControlBall = false;
 
     public Transform _cachedTrans;
 
     public bool _enableTouch = false;
     public float screenWidth;
     public float screenHeight;
 
     Vector3 _prePos, _curPos;
     public float angle;
     protected ScreenOrientation orientation;
 
     protected Transform _ballParent;
 
     protected RaycastHit _hit;
     public bool _isInTutorial = false;
     public Vector3 ballVelocity;
 
     private float _ballPostitionZ = -22f;
     private float _ballPostitionX = 0f;
 
     public float BallPositionZ
     {
         get { return _ballPostitionZ; }
         set { _ballPostitionZ = value; }
     }
 
     public float BallPositionX
     {
         get { return _ballPostitionX; }
         set { _ballPostitionX = value; }
     }
     public TrailRenderer _effect;
 
 
 
 
     protected virtual void Awake()
     {
         share = this;
         _cachedTrans = transform;
         _isShooting = true;
         _ballParent = _ball.transform.parent;
 
         _distanceMinX = -15f;
         _distanceMaxX = 15f;
         _distanceMaxZ = 30f;
 
     }
 
     // Use this for initialization
     protected virtual void Start()
     {
         //        Application.targetFrameRate = 30;
         _mainCam = CameraManager.share._cameraMainComponent;
 
 #if UNITY_WP8 || UNITY_ANDROID
         //Time.maximumDeltaTime = 0.2f;
         //Time.fixedDeltaTime = 0.008f;
         Time.maximumDeltaTime = 0.1f;
         Time.fixedDeltaTime = 0.005f;
 #else
         Time.maximumDeltaTime = 0.1f;
         Time.fixedDeltaTime = 0.005f;
 #endif
 
         orientation = Screen.orientation;
         calculateFactors();
 
         //_ballControlLimit = 6f;
         EventChangeBallLimit(_ballControlLimit);
 
         reset();
         CameraManager.share.reset();
         GoalKeeper.share.reset();
 
         GoalDetermine.EventFinishShoot += goalEvent;
     }
 
     void OnDestroy()
     {
         GoalDetermine.EventFinishShoot -= goalEvent;
     }
 
     public virtual void goalEvent(bool isGoal, Area area)
     {
         _canControlBall = false;
         _isShooting = false;
     }
 
     public void calculateFactors()
     {
         screenHeight = Screen.height;
         screenWidth = Screen.width;
 
         minDistance = (100 * screenHeight) / 960f;
         factorUp = factorUpConstant / screenHeight;
         factorDown = factorDownConstant / screenHeight;
         factorLeftRight = factorLeftRightConstant / screenWidth;
 
         Debug.Log("Orientation : " + orientation + "\t Screen height = " + screenHeight
             + "\t Screen width = " + screenWidth + "\t factorUp = " + factorUp + "\t factorDown = " + factorDown
             + "\t factorLeftRight = " + factorLeftRight + "\t minDistance = " + minDistance);
     }
 
     protected void LateUpdate()
     {
         if (screenHeight != Screen.height)
         {
             orientation = Screen.orientation;
             calculateFactors();
             CameraManager.share.reset();
         }
     }
     void FixedUpdate()
     {
         ballVelocity = _ball.velocity;
 
         Vector3 pos = _ball.transform.position;
         pos.y = 1.5f;
         _ballShadow.position = pos;
     }
 
     protected virtual void Update()
     {
         if (_isShooting)
         {      
             if (_enableTouch && !_isInTutorial)
             {
                 if (Input.GetMouseButtonDown(0))
                 {           // touch phase began
                     mouseBegin(Input.mousePosition);
                 }
                 else if (Input.GetMouseButton(0))
                 {
                     mouseMove(Input.mousePosition);
                 }
                 else if (Input.GetMouseButtonUp(0))
                 {   // touch ended
                     mouseEnd();
                 }
             }
             if (_isShoot)
             {
                 Vector3 speed = _ballParent.InverseTransformDirection(_ball.velocity);
                 speed.z = _zVelocity;
                 _ball.velocity = _ballParent.TransformDirection(speed);
             }
         }
     }
 
     public void mouseBegin(Vector3 pos)
     {
         _prePos = _curPos = pos;
         beginPos = _curPos;
     }
 
     public void mouseEnd()
     {
         if (_isShoot == true)
         {    
                 //_canControlBall = false;
         }
     }
 
     public void mouseMove(Vector3 pos)
     {
         if (_curPos != pos)
         {       // touch phase moved
             _prePos = _curPos;
             _curPos = pos;
 
 
             Vector3 distance = _curPos - beginPos;
 
             if (_isShoot == false)
             {               
                 if (distance.y > 0 && distance.magnitude >= minDistance)
                 {
                     if (Physics.Raycast(_mainCam.ScreenPointToRay(_curPos), out _hit, 100f) && !_hit.transform.tag.Equals("Ball"))
                     {
                         _isShoot = true;
 
                         Vector3 point1 = _hit.point;        // contact point
                         point1.y = 0;
                         point1 = _ball.transform.InverseTransformPoint(point1);    
                         point1 -= Vector3.zero;        
 
                         Vector3 diff = point1;
                         diff.Normalize();              
 
                         float angle = 90 - Mathf.Atan2(diff.z, diff.x) * Mathf.Rad2Deg;     
                            //Debug.Log("angle = " + angle);
 
                         float x = _zVelocity * Mathf.Tan(angle * Mathf.Deg2Rad);
 
                         //                            float x = distance.x * factorLeftRight;
                         _ball.velocity = _ballParent.TransformDirection(new Vector3(x, distance.y * factorUp, _zVelocity));
                         _ball.angularVelocity = new Vector3(0, x, 0f);
 
                         if (EventShoot != null)
                         {
                             EventShoot();
                         }
                     }
                 }
             }
             else
             {             
                 if (_canControlBall == true)
                 {  
 
                     if (_cachedTrans.position.z < -_ballControlLimit)
                     {
                      
                         distance = _curPos - _prePos;
 
                         Vector3 speed = _ballParent.InverseTransformDirection(_ball.velocity);
                         speed.y += distance.y * ((distance.y > 0) ? factorUp : factorDown);
                         speed.x += distance.x * factorLeftRight * factorLeftRightMultiply;
                         _ball.velocity = _ballParent.TransformDirection(speed);
 
                         speed = _ball.angularVelocity;
                         speed.y += distance.x * factorLeftRight;
                         _ball.angularVelocity = speed;
                     }
                     else
                     {
                         _canControlBall = false;
                     }
                 }
             }
         }
     }
 
     protected void OnCollisionEnter(Collision other)
     {
         string tag = other.gameObject.tag;
         if (tag.Equals("Player") || tag.Equals("Obstacle") || tag.Equals("Net") || tag.Equals("Wall"))
         {   
             _isShooting = false;
 
             if (tag.Equals("Net"))
             {
                 _ball.velocity /= 3f;
             }
         }
 
         EventOnCollisionEnter(other);
     }
 
     private void enableEffect()
     {
         //        _effect.enabled = true;
         _effect.time = 1;
     }
 
     public virtual void reset()
     {
         reset(-Random.Range(_distanceMinX, _distanceMaxX), -Random.Range(_distanceMinZ, _distanceMaxZ));
 
     }
 
     public virtual void reset(float x, float z)
     {
         Debug.Log(string.Format("<color=#c3ff55>Reset Ball Pos, x = {0}, z = {1}</color>", x, z));
 
         _effect.time = 0;
         //        _effect.enabled = false;
         Invoke("enableEffect", 0.1f);
 
         BallPositionX = x;
         EventChangeBallX(x);
         BallPositionZ = z;
         EventChangeBallZ(z);
 
 
         _canControlBall = true;
         _isShoot = false;
         _isShooting = true;
 
         // reset ball
         _ball.velocity = Vector3.zero;
         _ball.angularVelocity = Vector3.zero;
         _ball.transform.localEulerAngles = Vector3.zero;
 
 
         Vector3 pos = new Vector3(BallPositionX, 0f, BallPositionZ);
         Vector3 diff = -pos;
         diff.Normalize();
         float angleRadian = Mathf.Atan2(diff.z, diff.x);        // tinh' goc' lech
         float angle = 90 - angleRadian * Mathf.Rad2Deg;
 
         _ball.transform.parent.localEulerAngles = new Vector3(0, angle, 0);     // set parent cua ball xoay 1 do theo truc y = goc lech
 
         _ball.transform.position = new Vector3(BallPositionX, 0.16f, BallPositionZ);
 
         pos = _ballTarget.position;
         pos.x = 0;
         _ballTarget.position = pos;
 
         float val = (Mathf.Abs(_ball.transform.localPosition.z) - _distanceMinZ) / (_distanceMaxZ - _distanceMinZ);
         _zVelocity = Mathf.Lerp(_speedMin, _speedMax, val);
 
         EventChangeSpeedZ(_zVelocity);
 
         EventDidPrepareNewTurn();
     }
 
     public void enableTouch()
     {
         _enableTouch = true;
     }
 
     public void disableTouch()
     {
         StartCoroutine(_disableTouch());
     }
 
     private IEnumerator _disableTouch()
     {
         yield return new WaitForEndOfFrame();
         _enableTouch = 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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A touch'es state is always Began and the position doesn't change 1 Answer

How to show and hide an image by swiping 0 Answers

Detect touches on ui element 1 Answer

How to move objects with perspective camera and rotating the camera in 3D? 0 Answers

Reset touch Vector after movement 2 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