- Home /
Question by
skyxgamingbusiness8005 · Nov 25, 2019 at 07:55 AM ·
androidunity 5scripting problemuimobile
How to make Input.GetAxis work on certain area only?
Hi I'm trying to make my own android horror game. When swipe (input.GetAxis) the screen it will rotate the camera. Is work fine to me but after I add joystick to the game it work horrible. The problem is when i move the joystick it will also rotate my camera. I want my camera rotate when I swipe the sceen not by moveing the joystick. This is my camera code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraLightRotation : MonoBehaviour
{
public float speed = 3.5f;
private float X;
public float Y;
public bool Camera;
public Transform player;
public Transform cameraRotation;
public float MouseX;
public float MouseY;
void Update()
{
if (Input.GetMouseButton(0))
{
MouseX = Input.GetAxis("Mouse Y");
MouseY = Input.GetAxis("Mouse X");
transform.Rotate(new Vector3(-MouseX * speed, MouseY * speed, 0));
X = transform.rotation.eulerAngles.x;
Y = transform.rotation.eulerAngles.y;
cameraRotation.rotation = Quaternion.Euler(X, Y, 0);
}
}
void LateUpdate()
{
if (Camera)
{
transform.position = player.position + offset;
cameraRotation.rotation = Quaternion.Euler(X, Y, 0);
}
if (Flashlight)
{
transform.position = player.position;
transform.rotation = cameraRotation.rotation;
}
if (AreaLight)
{
transform.position = player.position;
}
}
public void PlayerIsRunningCameraShouldRotate()
{
if (Camera)
{
CameraRotateWhenRunning = GetComponent<JoystickPlayerMovement>().joystickDegree;
cameraRotation.rotation = Quaternion.Euler(0, Y + CameraRotateWhenRunning, 0);
}
}
}
This is my joystick code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class JoystickPlayerMovement : MonoBehaviour
{
protected Joystick joystick;
public float joystickDegree;
public Rigidbody rb;
//Get Component
public float PlayerCurrentStrenght;
public float PlayerCurrentSpeed;
//Get float
public float PlayerMaxStrenght;
public float PlayerMaxSpeed; //When walk
public float PlayerMaxFastestSpeed; //When run
//Regeneration & Reduce Speed
public float PlayerStrenghtRegenerationSpeed;
public float PlayerStrenghtReduceSpeed;
public bool PlayerRunning = false;
public float NormalForwardSpeed;
public float RunForwardSpeed;
public bool PlayerRunningTest = false;
public float H;
public float V;
public float CameraRotation; //Y
public GameObject TiredUI;
void Start()
{
TiredUI.SetActive(false);
joystick = FindObjectOfType<Joystick>();
PlayerMaxStrenght = PlayerPrefs.GetFloat("PlayerMaxStrenght", 100);
PlayerMaxSpeed = PlayerPrefs.GetFloat("PlayerMaxSpeed", 10);
NormalForwardSpeed = PlayerPrefs.GetFloat("NormalForwardSpeed", 200);
PlayerMaxFastestSpeed = PlayerPrefs.GetFloat("PlayerMaxFastestSpeed", 20);
RunForwardSpeed = PlayerPrefs.GetFloat("RunForwardSpeed", 400);
PlayerStrenghtRegenerationSpeed = PlayerPrefs.GetFloat("PlayerStrenghtRegenerationSpeed", 5);
PlayerStrenghtReduceSpeed = PlayerPrefs.GetFloat("PlayerStrenghtRedeuceSpeed", 20);
PlayerCurrentStrenght = PlayerMaxStrenght;
}
void Update()
{
//Add this so AddRelativeForce to work
CameraRotation = FindObjectOfType<CameraLightRotation>().Y;
rb.rotation = Quaternion.Euler(0, CameraRotation, 0);
PlayerCurrentSpeed = rb.velocity.magnitude;
rb = GetComponent<Rigidbody>();
H = joystick.Horizontal;
V = joystick.Vertical;
if (H != 0 && V != 0)
{
if (PlayerRunning == false)
{
if (PlayerCurrentSpeed <= PlayerMaxSpeed)
{
rb.AddRelativeForce(transform.forward * NormalForwardSpeed * V * Time.deltaTime);
rb.AddRelativeForce(transform.right * NormalForwardSpeed * H * Time.deltaTime);
}
}
else
{
if(PlayerCurrentStrenght > 0)
{
if (PlayerCurrentSpeed <= PlayerMaxFastestSpeed)
{
rb.AddRelativeForce(transform.forward * RunForwardSpeed * V * Time.deltaTime);
rb.AddRelativeForce(transform.right * RunForwardSpeed * H * Time.deltaTime);
//Reduce Strenght
PlayerCurrentStrenght -= PlayerStrenghtReduceSpeed * Time.deltaTime;
}
}
else //When player tired call UI & set running to false
{
PlayerRunning = false;
TiredUI.SetActive(true);
}
}
}
else //If joystick didnt move player stop moving & stop running
{
if(PlayerRunningTest == false)
{
PlayerRunning = false;
}
rb.AddForce(0, -100, 0);
}
//Regeneration
if(PlayerRunning == false)
{
if (PlayerCurrentStrenght != PlayerMaxStrenght)
{
PlayerCurrentStrenght += PlayerStrenghtRegenerationSpeed * Time.deltaTime;
}
}
}
public void RunPlayer()
{
if (PlayerRunning == false)
{
PlayerRunning = true;
}
else
{
PlayerRunning = false;
}
}
}
Could anyone try to help me? Any help will be appreciate.
Comment