I'm working on an online multiplayer fps game for android devices.
I have 12 player in my game , 6 player in one side and 6 player in other side and they kill each other.
Evrything is fine but there is an issue in my game and that is , When players are in the scene , Mouse look is laggy.
here is my code :
public void MyUpdate()
{
the_time = Time.deltaTime;
if (Input.touches.Length > 0)
{
foreach (Touch t in Input.touches)
{
if (t.position.x > Screen.width / 2)
{
if (t.phase == TouchPhase.Began)
{
delta = t.deltaPosition;
}
if (t.phase == TouchPhase.Began || t.phase == TouchPhase.Moved)
{
delta = -t.deltaPosition;
ROTX_ROTY.x += (delta.y * sensitivityX * current_speed_offset_vertical * the_time);
ROTX_ROTY.y -= delta.x * sensitivityY * current_speed_offset * the_time;
ROTX_ROTY.x = Mathf.Clamp(ROTX_ROTY.x, -clampAngle, clampAngle);
MyTransform.rotation = Quaternion.AngleAxis(ROTX_ROTY.y,Vector3.up);
x_rot_transform.localRotation = Quaternion.AngleAxis(ROTX_ROTY.x, Vector3.right);
}
else if (t.phase == TouchPhase.Ended)
{
delta = t.deltaPosition;
}
}
}
}
}
Anybody knows how can i solve this problem ?
Because this lag is only in my mouse look . Other things like walking, shooting works fine !
Is there any way to I optimize this code for mobiles?
For example I use Quaternion.AngleAxis instead of Quaternion.Euler because it's little faster... But after all this , I have lag every few second in my game.
/////////////////////
UPDATES :
I changed part of my code and change IF statement to switch case , but my problem not solved.
if (Input.touches.Length > 0)
{
foreach (Touch t in Input.touches)
{
if (t.position.x > Screen.width / 2)
{
switch (t.phase)
{
case TouchPhase.Moved:
delta = -t.deltaPosition;
ROTX_ROTY.x += (delta.y * sensitivityX * current_speed_offset_vertical * the_time);
ROTX_ROTY.y -= delta.x * sensitivityY * current_speed_offset * the_time;
ROTX_ROTY.x = Mathf.Clamp(ROTX_ROTY.x, -clampAngle, clampAngle);
MyTransform.rotation = Quaternion.AngleAxis(ROTX_ROTY.y, Vector3.up);
x_rot_transform.localRotation = Quaternion.AngleAxis(ROTX_ROTY.x, Vector3.right);
break;
default:
delta = t.deltaPosition;
break;
}
}
}
}
Related
I have problem with smooth map rotation. I use Sensor and onSensorChanged to get direction which side the user is looking at. Then I put it to math degrees (rage -180 : 180)
degres = Math.toDegrees(mOrientation.get(0).toDouble())
and then pass it to other function
if (gps != null) {
if(degres!! !in lastDegres!!-10..lastDegres!!+10 && degres!!>0 || degres!! !in lastDegres!!-10 ..lastDegres!!+10 && degres!!<0 ){
//here
lastDegres = degres
}
}
if statement avoid shaking map when sensor get a little movement.
My problem is what I have type in this if statement to smoothly rotate map. I already trying something like this :
if (gps != null) {
if(degres!! !in lastDegres!!-10..lastDegres!!+10 && degres!!>0 || degres!! !in lastDegres!!-10 ..lastDegres!!+10 && degres!!<0 ){
var result = (lastDegres!! - (degres!! )).toInt()
for (i in 0..result) {
setOrientation(((180 + lastDegres!! + i ).toFloat()))
update()
}
lastDegres = degres
}
}
but it's work only in only right direction, any advices how to allow to rotate smoothly to left and right ?
// ANSWER
if u want use this next step is find closest site left or right to actually position
if (gps != null) {
realpos = degres!! * -1
if (realpos < 0) realpos += 360
dif = if (realpos > lastDegres!!) { realpos - lastDegres!! } else { (lastDegres!! - realpos) }
if (dif > 10) {
if(realpos > lastDegres!!){
for (i in 1..dif.toInt()*50000) {
orientation = (lastDegres!!.toFloat()+i/50000)
update()
}
}else if (lastDegres!! > realpos) {
for (i in 1..dif.toInt()*50000){
orientation = (lastDegres!!.toFloat()-i/50000)
update()
}
}
lastDegres = realpos
}
Right now I've been developing a game which will make the character move up or down if either swipe up or down on the left side of the screen. Then, he can also, shoot projectiles if I tap the right side of the screen. I've succeeded on making it work but the problem is that the swipe function do not works while I tap the the right screen. Both only works if I do one of them. I cannot move the character if I am firing projectiles. Any suggestions? Appreciate it in advanced.
Here's the code:
For the swipe movement function:
void Swipe() {
if (Input.touchCount > 0) {
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
lp = t.position;
fp = t.position;
text.text = fp.x.ToString();
}
else if (t.phase == TouchPhase.Ended)
{
lp = t.position;
//Swipe Up
if (fp.y < lp.y && fp.x < 400)
{
currentLane += 1;
}
//Swipe Down
else if (fp.y > lp.y && fp.x < 400)
{
currentLane -= 1;
}
}
}
}
And here's the code for the tap or firing projectiles function:
void FireBullets() {
interval += 2 * Time.deltaTime;
anim.SetBool("Attacking", firing);
if (Input.touchCount > 0 && interval > .75f) {
Vector3 bulletTouchPos;
Touch bulletT = Input.GetTouch(0);
bulletTouchPos = bulletT.position;
if (bulletT.phase == TouchPhase.Began) {
if (bulletTouchPos.x > 400)
{
interval = 0;
firing = true;
//Fire Bullets
Instantiate(bullets, new Vector3(transform.position.x + 1.2f, transform.position.y + .3f, transform.position.z), Quaternion.identity);
}
}
}else
{
firing = false;
}
}
Your lp and fp values don't care which finger is being checked.
If you want to detect multiple touch gestures at once, you need to discriminate your detection based on which finger that is.
You can do this by looking at the Touch.fingerId value, which is unique for each finger. Fingers are just an ID assigned to a given touching point as it moves across the screen in order to identify it from other touching points currently also on the screen.
You'll need to adjust your code to handle how to store the information you need in order to do what you need, but this should get you started.
I'm doing a game to android with Unity and I don't know how to do to jump more or less depending how much time you press the screen, I tried it but I don't know how to do it, I used deltatime but it doesn't work, at least no at least not in the way I did it, so I would like to know how to do that. The character jumps but just a little bit, it doesn't matter how much time I press the screen, it jumps so low.
This is how I've tried to accomplish that:
void Update () {
movimiento ();
if (transform.position.x <= 4.65f) {
SceneManager.LoadScene ("Game Over");
}
if (Input.touchCount > 0) {
GetComponent<Animator> ().Play ("Andar 2");
print (Input.GetTouch (0).deltaTime);
if (Input.GetTouch (0).deltaTime >= 2) {
GetComponent<Rigidbody2D> ().AddForce (Vector3.up * Time.deltaTime * 20000);
GetComponent<Animator> ().Play ("Andar 2");
} else if (Input.GetTouch (0).deltaTime >= 1) {
GetComponent<Rigidbody2D> ().AddForce (Vector3.up * Time.deltaTime * 2000);
} else if (Input.GetTouch (0).deltaTime < 1) {
GetComponent<Rigidbody2D> ().AddForce (Vector3.up * Time.deltaTime * 200);
}
}
}
I like to work with velocities directly when doing jumps as i feel like i can be more precise with it, But regardless, here is my solution for variable height jumps.
void Update ()
{
// set jump controls
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
jump = true;
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && !grounded)
jumpCancel = true;
}
void FixedUpdate()
{
// if player presses jump
if (jump)
{
rigidbody.velocity = new Vector3(rigidbody.velocity.x, jumpVelocity, rigidbody.velocity.z);
jump = false;
}
// if player performes a jump cancel
if (jumpCancel)
{
if (rigidbody.velocity.y > shortJumpVelocity)
rigidbody.velocity = new Vector3(rigidbody.velocity.x, shortJumpVelocity, rigidbody.velocity.z);
controller.jumpCancel = false;
}
}
This works by altering the player's velocity if the finger is lifted before the shortJumpVelocity is reached. As you can see you will have to have a grounded state of some kind for this to work properly.
I love this method as it avoids timers and gives the player a lot of control.
I have been making endless jumping game.
It is lacking powerbar.. I have managed to make one but it does not scale with the screen change.. I tried to use canvas to world screen but bar won't face the direction you click..
So like the longer you hold left mouse click, more power it has and it faces the direction you click.. Atleast it should do that, and i does but only on one screen size.
It looks likes this:
http://prntscr.com/b6z5wn
So this is the code i am currently using..
function Start ()
{
powerFill_startPos = powerFill.transform.localPosition.x;
powerFillAccel = 770 * Time.deltaTime;
}
function Update ()
{
if(Input.GetMouseButton(0) && collision.onPlatform == true && delete.jurinoFlag == true)
{
bar_back.enabled = true;
powerFill_Image.enabled = true;
var moveDirection : Vector3 = gameObject.transform.position - Input.mousePosition;
if (moveDirection != Vector3.zero)
{
var angle : float = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
angle -= 180;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
powerFill.transform.localPosition.x += powerFillAccel;
}
else
{
bar_back.enabled = false;
powerFill.transform.localPosition.x = powerFill_startPos;
powerFill_Image.enabled = false;
}
}
I would very much appreciate any help i can get so i can finish the game and publish it :)
Thanks for taking time and replying. Have a nice day!
I am trying to create a RTS style game with a camera system similar to Warcraft/Starcraft and the like. I am making the game for Android/iOS. As of now I found two ways to make the camera go where I want it to with touch input. The problem is that either way the camera will move as it should for about a second, then slow down significantly like it is stuck, then speed up again. This process continues and repeats as long as the user is moving the camera. I have tried to put my code in an Update(), FixedUpdate(), and LateUpdate() function. None of this seems to help. I have included a link below to some code which includes both methods, one of which is commented out. As for the reference to "cameraGuide" in the comment near the top, that is an empty game object I placed at (0,0,0) and made the camera as a child of it. In one of the methods the camera must have the script so that when i use transform.localPosition it refers to the cameras position. In the other method the script must be on the empty game object because that is what is actually translated around.
I should also mention that this only occurs when I test my app while it is installed on the Android device itself. When I use Unity Remote to my device it never happens. I have an HTC One M8 running Android 4.4.2.
here is the code:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
float dragSpeed = 3f;
Vector3 movement = new Vector3(0, 0, 0);
Vector2 deltaPosition;
//script should be on cameraGuide for this code
void Update() {
if (Input.touchCount > 0) {
if (Input.touchCount == 1) {
switch (Input.GetTouch(0).phase) {
case TouchPhase.Began:
break;
case TouchPhase.Moved:
deltaPosition = Input.GetTouch(0).deltaPosition;
movement.x = -deltaPosition.x;
movement.z = -deltaPosition.y;
transform.Translate((movement.x) * dragSpeed * Time.deltaTime, 0, movement.z * dragSpeed * Time.deltaTime);
//if (transform.position.z >= 450 && (movement.z > 0 || movement.x < 0)) {
// transform.Translate(0, 0, 0, Space.Self);
//} else {
//transform.Translate((movement.x) * dragSpeed, 0, movement.z * dragSpeed, Space.Self);
//}
break;
case TouchPhase.Ended:
break;
}
} else if (Input.touchCount == 2) {
Debug.Log("pinching");
}
}
}
//script should be on the camera for this code
//float dragSpeed = 5f;
// Vector3 movement = new Vector3(0, 0, 0);
// Vector2 deltaPosition;
// void Update() {
// if (Input.touchCount > 0) {
// if (Input.touchCount == 1) {
// switch (Input.GetTouch(0).phase) {
// case TouchPhase.Began:
// break;
// case TouchPhase.Moved:
// deltaPosition = Input.GetTouch(0).deltaPosition;
// movement.x = transform.localPosition.x - deltaPosition.x;
// movement.y = transform.localPosition.y;
// movement.z = transform.localPosition.z - deltaPosition.y;
// StartCoroutine(TransitionCamera());
// //transform.localPosition = Vector3.Lerp(transform.position, movement, dragSpeed);
// //transform.Translate((movement.x) * dragSpeed * Time.deltaTime, 0, movement.z * dragSpeed * Time.deltaTime);
// break;
// case TouchPhase.Ended:
// break;
// }
// } else if (Input.touchCount == 2) {
// Debug.Log("pinching");
// }
// }
// }
// IEnumerator TransitionCamera() {
// var t = 0f;
// if (t < 1) {
// t += Time.deltaTime;
// transform.localPosition = Vector3.Lerp(transform.localPosition, movement, t * dragSpeed);
// yield return null;
// }
// }
}
Thank you for any help.