- Home /
C# Screen.width Touch Input In Multiple Scripts Affect Each Other - Please Help
Hi guys, I'm have a small problem with my touch input code for mobile devices. What I have is a character that jumps when you touch the left of the screen and shoots his gun when you touch the right of the screen.
At the moment this is working fine, but I have these in separate scripts, so when I press the jump side, the shoot side is picking up the input and ends up changing the direction that he was aiming.
This is the left side's script (player.cs):
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began && touch.position.x < Screen.width / 2)
{
isJumping = true;
}
}
This is the right side's script (shoot.cs):
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began && touch.position.x > Screen.width/2)
{
shootActive = true;
}
}
Is there anything you notice that I'm not doing or something? I'd really appreciate the help, thanks a lot!
Answer by L_Artista · Jan 11, 2016 at 06:29 AM
It will be better if they are in one script
Shoot shootScript;
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
if(touch.position.x < Screen.width / 2)
{
isJumping = true;
}
else if(touch.position.x > Screen.width/2)
{
shootScript.shootActive = true; //
}
}
}
Hey thanks for that I really appreciate it! I didn't know you could reference other scripts when using Input's.
I've tried it though, and it's still having that issue. Furthermore it's not shooting the projectile when touching the right of the screen anymore, it's only ai$$anonymous$$g the gun in the correct location.
Any other ideas?
I just figured out why it wouldn't shoot, it's because I have the shoot script linked to a separate object, so I replaced your shootScript.shootActive = true;
with GameObject.FindGameObjectWithTag("Cannon").GetComponent<Shoot>().mouseDown = true;
.
And I realize why I'm getting the issue with the ai$$anonymous$$g, it's actually because it thinks the ai$$anonymous$$g is a mouse location, and not a touch location, it was in a completely different script that I forgot about. It's in JavaScript though because I couldn't find a method that worked in C#.
Do you know how I could replace the mouse position with touch position ins$$anonymous$$d?
function Update ()
{
mouse_pos = Input.mousePosition;
mouse_pos.z = 5.23; //The distance between the camera and object
object_pos = Camera.main.WorldToScreenPoint(target.position);
mouse_pos.x = mouse_pos.x - object_pos.x;
mouse_pos.y = mouse_pos.y - object_pos.y;
angle = $$anonymous$$athf.Atan2(mouse_pos.y, mouse_pos.x) * $$anonymous$$athf.Rad2Deg;
angle = $$anonymous$$athf.Clamp(angle, -22, 40);
transform.rotation = Quaternion.Euler(Vector3(0, 0, angle));
}
Alright never$$anonymous$$d, I solved the rest of my issues, lol.
For reference replaced mouse_pos = Input.mousePosition;
with:
for (var touch: Touch in Input.touches) {
if (touch.position.x > Screen.width / 2){
mouse_pos = Input.touches[0].position;
$$anonymous$$arking your answer as correct, thanks for the help!
Your answer
Follow this Question
Related Questions
Gravity Switch Touch 1 Answer
Mobile game Player follow finger input problem (C#) 2 Answers
Convert Touch input to Mouse input C#? 1 Answer
How to convert Touch Input To Mouse Input C# unity? 1 Answer
Input.touchSupported doesn't exist? 0 Answers