Unity Multi touch problems with WebGL - unity3d

i'm trying to implement a simple orbit camea that can rotate and zoom in a scene. I followed some tutorials and i can run my program without any problems on windows if i build it as a PC Standalone. If i build it as WebGL my camera still correctly works with a single touch (rotation) , with multi touch i have a strange behaviour.. Difficult to explain but it zoomsin and out even if i do not move the fingers. Is king of "fuzzy". It happens with Firefox and Chrome, the browsers i have tested.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraOrbitTouch : MonoBehaviour {
protected Transform _XForm_Camera;
protected Transform _XForm_Parent;
protected Vector3 _LocalRotation;
protected float _CameraDistance = 10f;
// The rate of change of the field of view in perspective mode.
public float perspectiveZoomSpeed = 0.2f;
public float OrbitDampening = 30f;
public float ScrollDampening = 18f;
public bool CameraDisabled = false;
// Use this for initialization
void Start() {
this._XForm_Camera = this.transform;
this._XForm_Parent = this.transform.parent;
}
void LateUpdate() {
if (Input.GetKeyDown(KeyCode.LeftShift))
CameraDisabled = !CameraDisabled;
if (!CameraDisabled)
{
//Rotation of the Camera based on Mouse Coordinates
if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
_LocalRotation.x += Input.touches[0].deltaPosition.x ;
_LocalRotation.y += Input.touches[0].deltaPosition.y;
//Clamp the y Rotation to horizon and not flipping over at the top
if (_LocalRotation.y < 0f)
_LocalRotation.y = 0f;
else if (_LocalRotation.y > 90f)
_LocalRotation.y = 90f;
}
//Zooming Input from our Mouse Scroll Wheel
if (Input.touchCount == 2 && Input.GetTouch(0).phase == TouchPhase.Moved && Input.GetTouch(1).phase == TouchPhase.Moved)
{
// Store both touches.
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
// Find the position in the previous frame of each touch.
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
// Find the magnitude of the vector (the distance) between the touches in each frame.
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
// Find the difference in the distances between each frame.
float deltaMagnitudeDiff = (prevTouchDeltaMag - touchDeltaMag);
//this._CameraDistance += deltaMagnitudeDiff * -1f;
this._CameraDistance += deltaMagnitudeDiff * perspectiveZoomSpeed;
this._CameraDistance = Mathf.Clamp(this._CameraDistance, 1.5f, 100f);
}
}
//Actual Camera Rig Transformations
Quaternion QT = Quaternion.Euler(_LocalRotation.y, _LocalRotation.x, 0);
this._XForm_Parent.rotation = Quaternion.Lerp(this._XForm_Parent.rotation, QT, Time.deltaTime * OrbitDampening);
if ( this._XForm_Camera.localPosition.z != this._CameraDistance * -1f )
{
this._XForm_Camera.localPosition = new Vector3(0f, 0f, Mathf.Lerp(this._XForm_Camera.localPosition.z, this._CameraDistance * -1f, Time.deltaTime * ScrollDampening));
}
}
}
Can someone can tell me why this is happening? Have my code some errors? There is some kind of incompatibility with WebGL and multi touch? Thans for your time and answers

Ok i found the problem. The problem was Windows 7. I used the same code on another computer with windows 10 and it works as intended...

Related

Unity3D 2D Simple Flight Physics

I want to create a simple flight physics sim, arcade-y like game, in 2D.
I don't want something too realistic, just something fun.
Unfortunately, my code leads to a game, where my plane acts more like a missile in space, than an aerodynamic plane.
I've checked for questions around the internet, the closest one I found was this.
Which the guy wrote about this game.
The game looks a bit like what I want to do.
I couldn't find any practical code I can use to make my plane more realistic, so I'm looking for help.
Please, do not send me to learn aerodynamics or to something not useful, I do have a simple understanding, and I would like some practical help, not theoretical one.
The main issue I have, is when I want to pitch up/down the plane, the plane just rotates itself with the Torque force, however, it doesn't change it's direction quickly with the air flow (since there's no 'real' air flow...)
I've tried playing with the rigidbody drag, and angular drag, and the parameters, I've added a drag force of my own... I am missing something and I don't know what. I was also looking at these formulas, I didn't find the solution.
Thanks in advance,
Here is my code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaneController : MonoBehaviour {
public float thrustFactor = 0.01f;
public float maxThrustFactor = 1.0f;
public float liftFactor = 0.1f;
public float turnFactor = 0.1f;
public float gravityFactor = 0.1f;
public float dragFactor = 0.1f;
public float thrust;
public float liftForce;
private Vector2 planeVelocity;
private Rigidbody2D transformRigidbody;
// Use this for initialization
void Start () {
thrust = 0;
liftForce = 0;
transformRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
planeVelocity = transformRigidbody.velocity;
PlaneGravity();
PlaneThrust();
PlaneDrag();
PlaneTurn();
PlaneLift();
}
void PlaneLift()
{
float attackAngle = transform.eulerAngles.z;
if ((attackAngle > 0 && attackAngle <= 45) || (attackAngle > 90 && attackAngle <= 135))
liftForce = Mathf.Sin(attackAngle) * liftFactor * planeVelocity.sqrMagnitude;
else if ((attackAngle > 45 && attackAngle <= 90) || (attackAngle > 135 && attackAngle <= 180))
liftForce = Mathf.Cos(attackAngle) * liftFactor * planeVelocity.sqrMagnitude;
transformRigidbody.AddForce(Vector2.up * Mathf.Abs(liftForce) * Time.fixedDeltaTime);
}
void PlaneThrust()
{
if (Input.GetKey("w"))
{
thrust += thrustFactor;
thrust = Mathf.Min(thrust, maxThrustFactor);
}
if (Input.GetKey("s"))
{
thrust -= thrustFactor;
thrust = Mathf.Max(0, thrust);
}
transformRigidbody.AddForce(transform.right * thrust * Time.fixedDeltaTime);
}
void PlaneGravity()
{
transformRigidbody.AddForce(gravityFactor * Vector2.down * Time.fixedDeltaTime);
}
void PlaneDrag()
{
transformRigidbody.AddForce(-transform.right * planeVelocity.sqrMagnitude * Time.fixedDeltaTime);
}
void PlaneTurn()
{
if (Input.GetKey("up"))
{
transformRigidbody.AddTorque(-turnFactor * Mathf.Abs(planeVelocity.sqrMagnitude) * Time.fixedDeltaTime);
}
if (Input.GetKey("down"))
{
transformRigidbody.AddTorque(turnFactor * Mathf.Abs(planeVelocity.sqrMagnitude) * Time.fixedDeltaTime);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
Vector3 newPosition = transform.position;
if (other.name == "LeftCollider")
{
newPosition.x += 17;
}
if (other.name == "RightCollider")
{
newPosition.x -= 17;
}
transform.SetPositionAndRotation(newPosition, transform.rotation);
}
private void OnCollisionExit2D(Collision2D collision)
{
if(collision.transform.name == "Ground")
{
transformRigidbody.freezeRotation = false;
}
}
}

How can i get the movement of an object to follow the y co-ordinate movement of another game object

I'm trying to program AI for a pong game and i'm trying to make the paddle game object follow the y co-ordinate movement of the ball game object.
The problem is that the paddle ends up just moving up and down and not actually following the ball.
This is the script i used to make it follow.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AIMove : MonoBehaviour {
public int speed;
public bool validUp = true;
public bool validDown = true;
void Awake()
{
}
void Update()
{
GameObject sphere = GameObject.Find("Sphere");
Transform spherePosition = sphere.GetComponent<Transform>();
float spherePos = spherePosition.position.y;
if (spherePos < (0) && validUp == (true))
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
else if (spherePos > (0) && validDown == (true))
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
}
if (transform.position.y >= (2.3F))
{
validUp = false;
}
else if (transform.position.y <= (-2.3F))
{
validDown = false;
}
else
{
validUp = true;
validDown = true;
}
}
}
I think you do not fully understand how your code works. Let's take a look inside your Update method.
if (spherePos < (0) && validUp == (true))
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
What you are saying here is that if the position of the sphere on the Y axis is below 0, the AI paddle should move up. This contains no condition on whether the sphere is moving upwards or downwards. So, the sphere could be moving downwards, and you would still be moving the AI paddle upwards.
else if (spherePos > (0) && validDown == (true))
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
}
This contains the same kind of problems.
Instead, you could do something like this.
[SerializeField]
private float speed;
private Transform sphere;
private void Start()
{
// In general, I'd recommend avoiding
// GameObject.Find, but we'll use it for now.
sphere = GameObject.Find("Sphere").transform;
}
private void Update()
{
if(sphere.position.y >= transform.position.y)
{
transform.Translate(Vector.up * speed * Time.deltaTime);
}
else if(sphere.position.y <= transform.position.y)
{
transform.Translate(Vector.down * speed * Time.deltatime);
}
}
This would basically compare the paddle's position on the Y axis to the sphere's position on the Y axis and move the paddle accordingly, to the correct direction. Then you just have to adjust the speed value as you want.
Note that this only moves the paddle based on the sphere's position on the Y axis alone. It does not take into account how the sphere is going to bounce off the walls or anything like that.

Unity3D How to make object go away form mouse click position 2D

Im thinking about it two days. I still did not made any progress.
I wonder how to do that objects fly away from mouse click position in 2D view?
I tried like that:
pos = Input.mousePosition;
Vector3 realWorldPos = Camera.main.ScreenToViewportPoint(pos);
print("MOuse pos: " + realWorldPos);
//print(realWorldPos);
Vector3 velo = GetComponent<Rigidbody2D>().velocity;
if (realWorldPos.x < 0.5)
{
velo = new Vector3((realWorldPos.x * speed), velo.y);
}
else if(realWorldPos.x > 0.5)
{
velo = new Vector3((realWorldPos.x * speed) * (-1), velo.y);
}
if (realWorldPos.y < 0.5)
{
velo = new Vector3(velo.x, realWorldPos.y * speed);
}
else if (realWorldPos.y > 0.5)
{
velo = new Vector3(velo.x, (realWorldPos.y * speed) * (-1));
}
GetComponent<Rigidbody2D>().velocity = velo;
But it doesnt work as I want.
Is it possible to do this?
For this to work your Rigidbody2D must have Gravity Scale set to 0.
This is a simple test code that works for me, is placed on a sprite object:
public class PushPlayer : MonoBehaviour
{
public float pushPower = 50.0f;
Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
Vector3 dir = transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);
dir = dir.normalized;
rb.AddForce(dir * pushPower, ForceMode2D.Force);
// as alternative:
rb.velocity = dir * pushPower;
}
}
}
You need to adjust the values a bit, also in the regidbody (like drag) to get it the way you want.
Edit:
transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition): calculate the directional vector from the mouse position to the player position (have a look a vector algebra if you are not familiar with this) which is the direction away from the click (in a straight line).
dir.normalized: this shortens the vector to a length (= magnitude) of 1 (again have a look at vectors) so it really is just a direction. You could omit this and reduce the factor, but doing it this way your factor equals the force you use.

Level Selection menu, camera boundary unity 2d

My level selection menu scene will be a graphic map. Think level selection similar to candy crush. I have different sprites for different maps that will be instantiated when the user moves the camera to that map. In order for me to achieve this, I scroll/move the camera using touch on it's Y-axis. But the problem is, I'm having a trouble getting the boundary of the map sprite, I already read a lot of solution about this one. Here is my current script. I don't know what I'm doing wrong. My camera got stuck when it is near the boundary. Thank you for your help
public float scrollSpeed = 10.0f;
public GameObject[] maps;
Camera mainCamera;
private float topBound;
private float bottomBound;
private Vector3 pos;
private SpriteRenderer currentSprite;
private int mapIndex;
void Awake ()
{
mapIndex = 0;
mainCamera = Camera.main;
currentSprite = maps [mapIndex].GetComponent<SpriteRenderer> ();
bottomBound = currentSprite.sprite.bounds.size.y * -1;
topBound = currentSprite.sprite.bounds.size.y + currentSprite.gameObject.transform.position.y;
}
void Update ()
{
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
mainCamera.transform.Translate(0f,-touchDeltaPosition.y * scrollSpeed * Time.deltaTime,0f);
}
}
private float getAxisDelta(float axisDelta,float camMin,float camMax,float bottom,float top)
{
//get where edge of camera will have moved if full translate is done
float boundaryMinPixelsDestination = camMin - Mathf.Abs(axisDelta);
float boundaryMaxPixelsDestination = camMax + Mathf.Abs(axisDelta);
Debug.Log("MaxPixels:"+boundaryMaxPixelsDestination+"="+camMax+"+"+Mathf.Abs(axisDelta));
//check to see if you're within the border
if ((boundaryMinPixelsDestination <= bottom && axisDelta > 0) ||
(boundaryMaxPixelsDestination >= top && axisDelta < 0))
{
axisDelta = 0;
}
return axisDelta;
}
private float camBoundPos(string pos)
{
float bounds = 0f;
if (pos == "top")
bounds = mainCamera.transform.position.y + mainCamera.orthographicSize;
if(pos == "bottom")
bounds = mainCamera.transform.position.y - mainCamera.orthographicSize;
return bounds;
}

Unity3d - Moving camera literally cuts FPS in half?

I'm having an issue with the cameras in Unity. When the camera moves through any means it seems to cut my FPS in half if not more. It's not really noticeable on PC, unless I'm looking at the from from 800fps to about 150fps, however on mobile it'll cut the smooth 60fps I'm getting to 20fps on a Nexus 4. It's absolutely devastating.
Here's the properties for the camera I'm using & the script HOWEVER this issue still happens without ANY of these components and a completely reset camera component:
public class ViewDrag : MonoBehaviour
{
public Vector3 hit_position = Vector3.zero;
public Vector3 current_position = Vector3.zero;
public Vector3 camera_position = Vector3.zero;
public Vector2 min_position;
public Vector2 max_position;
float z = 0.0f;
MouseHolder holder;
hider sidebarHide;
GameObject gameStructure;
// Use this for initialization
void Start()
{
gameStructure = GameObject.FindGameObjectWithTag("GameStructure");
holder = gameStructure.GetComponent<MouseHolder>();
sidebarHide = GameObject.FindGameObjectWithTag("SidebarBG").GetComponent<hider>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
hit_position = Input.mousePosition;
camera_position = transform.position;
}
if (Input.GetMouseButton(0))
{
current_position = Input.mousePosition;
LeftMouseDrag();
}
if (!sidebarHide.isHidden)
{
//GetComponent<Camera2DFollow>().enabled = true;
}
if(gameStructure.GetComponent<ExecuteMovement2>().isExecuted)
{
//GetComponent<Camera2DFollow>().enabled = true;
}
}
void LeftMouseDrag()
{
// From the Unity3D docs: "The z position is in world units from the camera." In my case I'm using the y-axis as height
// with my camera facing back down the y-axis. You can ignore this when the camera is orthograhic.
//current_position.z = hit_position.z = camera_position.y;
// Get direction of movement. (Note: Don't normalize, the magnitude of change is going to be Vector3.Distance(current_position-hit_position)
// anyways.
Vector3 direction = Camera.main.ScreenToWorldPoint(current_position) - Camera.main.ScreenToWorldPoint(hit_position);
// Invert direction to that terrain appears to move with the mouse.
direction = direction * -1;
Vector3 position = camera_position + direction;
if (position.x < max_position.x && position.x > min_position.x && position.y < max_position.y && position.y > min_position.y)
{
if (!EventSystem.current.IsPointerOverGameObject() && holder.fromSlot == null )
{
if (sidebarHide.isHidden)
{
if (!gameStructure.GetComponent<ExecuteMovement2>().isExecuted)
{
//GetComponent<Camera2DFollow>().enabled = false;
transform.position = position;
}
}
}
}
}
}
Has anyone got an idea why this happens and how I can work around it if not fix it?
Through closer inspection I think it has something to do with the Canvas being screen space.. But it's kind of needed that way. Again, any workaround?
Check comments for profiler screenshot.
Problem Solved:
In the profiler I found that Canvas.SendWillRenderCanvases() was causing huge spikes. I solved the issue completely by turning off Pixel Perfect in the Canvas. Smooth as butter now.