Extending cross platform input not working - c#

I am trying to extend cross platform input in order to make mobile buttons to move a character left and right. What I did was I created two buttons and a script attached to each called MobileMovementButtons.cs. I run the program and the character moves left just fine but not right... I think it has something to do with my Conditional statement..
Here is my Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.EventSystems;
namespace CrossPlatformInput {
public class MobileMovementButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler{
public enum Direction {
Left,
Right
}
public Direction buttonDirection = Direction.Left;
CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis;
private bool rightPressed = false;
private bool leftPressed = false;
void Awake() {
CrossPlatformInputManager.UnRegisterVirtualAxis ("Horizontal");
}
void OnEnable() {
m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis("Horizontal" );
CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
}
void Update() {
if (rightPressed) {
m_HorizontalVirtualAxis.Update (1);
}
if (leftPressed) {
m_HorizontalVirtualAxis.Update (-1);
}
if (!leftPressed && !rightPressed){
m_HorizontalVirtualAxis.Update (0);
}
}
public void OnPointerDown(PointerEventData eD) {
if (this.buttonDirection == Direction.Left) {
leftPressed = true;
}
if (this.buttonDirection == Direction.Right) {
rightPressed = true;
}
}
public void OnPointerUp(PointerEventData eD) {
if (this.buttonDirection == Direction.Left) {
leftPressed = false;
}
if (this.buttonDirection == Direction.Right) {
rightPressed = false;
}
}
}
}
And here is the buttons:

Related

Unity change player PhotonNetwork Room with two object with collider

I'm looking for a solution of the following issue:
I have a player instantiated in lobby room at initial scene. There are two platform objects with colliders, I need to make one platform to join to one room with a scene and other platform to another room with another scene. I have attached the same script for both of platform with a boolean value which determines to which room to join. Instead of determine to which room to join it triggers both scripts. I need to work it in this way:
Platform A:
private bool teleportToShop = true;
If (teleportToShop) -> join shop room
Platform B:
private bool teleportToShop = false;
If (!teleportToShop) -> join event room
Here's my code attached to both game objects:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Portal : MonoBehaviourPunCallbacks
{
[SerializeField] private bool teleportToShop;
private float portalTime = 3f;
private string EVENT_HALL_TAG = "Cinema Hall";
private string LOBBY_TAG = "Lobby";
private string SHOP_TAG = "Shop";
private int currentRoom = 0;
private string nextRoom;
private PhotonView portalView;
private void Awake()
{
portalView = GetComponent<PhotonView>();
}
private void OnTriggerStay(Collider other)
{
if (portalView.IsMine)
{
if (other.GetComponent<PhotonView>().IsMine)
{
if (other.gameObject.tag == "Player")
{
if (portalTime > 0)
{
portalTime -= Time.deltaTime;
}
else
{
portalTime = 3f;
if (SceneManager.GetActiveScene().name == EVENT_HALL_TAG || SceneManager.GetActiveScene().name == SHOP_TAG)
{
currentRoom = 1;
PhotonNetwork.LeaveRoom();
}
else
{
currentRoom = 0;
PhotonNetwork.LeaveRoom();
}
}
}
}
}
}
private void OnTriggerExit(Collider other)
{
portalTime = 3f;
}
public override void OnConnectedToMaster()
{
PhotonNetwork.AutomaticallySyncScene = false;
PhotonNetwork.JoinLobby();
}
public override void OnJoinedLobby()
{
if (currentRoom == 0)
{
if (!teleportToShop)
{
PhotonNetwork.JoinRoom("EventHallRoom");
}
else if (teleportToShop)
{
PhotonNetwork.JoinRoom("ShopRoom");
}
}
else PhotonNetwork.JoinRoom("LobbyRoom");
}
public override void OnJoinRoomFailed(short returnCode, string message)
{
base.OnJoinRandomFailed(returnCode, message);
if (currentRoom == 0)
{
if (!teleportToShop)
{
PhotonNetwork.CreateRoom("EventHallRoom");
}
else if (teleportToShop)
{
PhotonNetwork.CreateRoom("ShopRoom");
}
}
else PhotonNetwork.CreateRoom("LobbyRoom");
}
public override void OnJoinedRoom()
{
if (currentRoom == 0)
{
if (!teleportToShop)
{
PhotonNetwork.LoadLevel(EVENT_HALL_TAG);
}
else if (teleportToShop)
{
PhotonNetwork.LoadLevel(SHOP_TAG);
}
}
else PhotonNetwork.LoadLevel(LOBBY_TAG);
}
public override void OnLeftRoom()
{
GameManager.currentPlayer = null;
}
}

in the my code if repeats repeatedly but but it shouldn't be

I press escape, in the debug it says that Pause(), Resum(), Pause() completed. And during the pause, Resum(), Pause(), Resum(), Pause() increase. During the dialogue, I press escape more to turn off the dialogue and still pause. How can I make sure that only one action is performed, and not several different ones? RETURN NOT WORKING
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused;
public DialogWindow dialogWindow;
public GameObject pauseMenuUI;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Debug.Log("resume");
Resume();
}
if (DialogWindow.IsDialog)
{
Debug.Log("dialog");
dialogWindow.Close();
}
else
{
Debug.Log("pause");
Pause();
}
}
}
public void Resume()
{
Cursor.lockState = CursorLockMode.Locked;
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
public void Pause()
{
Cursor.lockState = CursorLockMode.None;
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
public void ToMainMenu(int sceneNumber)
{
SceneManager.LoadScene(sceneNumber);
}
}
Perhaps you could use else if instead of just else, that would eliminate any chance of the different if statements to trigger simultaneously.
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Debug.Log("resume");
Resume();
}
else if (DialogWindow.IsDialog)
{
Debug.Log("dialog");
dialogWindow.Close();
}
else
{
Debug.Log("pause");
Pause();
}
}

Despite the name of the file and the name of the class being the same, Unity still can't find the class

Despite the many times I have rewritten the file and made sure the class and the file name match up unity is still saying it can't find the script class. I even made sure all the spelling important words were correct, and it still says it can't find the script class. The name of the file is BasicAI.cs This is what the file looks like.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace UnityStandardAssets.Characters.ThirdPerson
{
public class BasicAI : MonoBehaviour
{
public UnityEngine.AI.NavMeshAgent agent;
public ThirdPersonCharacteracter character;
public enum State {
PATROL,
CHASE
}
public State state;
private bool alive;
//Patrolling
public GameObject[] markers;
private int markerID=0;
public float patrolSpeed=0.0f;
//Chasing
public float chaseSpeed=1f;
public GameObject target;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
character = GetComponent<ThirdPersoncharacter>();
agent.updatePosition = true;
agent.updateRotation = false;
state = BasicAI.State.PATROL;
alive = true;
StartCoroutine("FiniteState");
}
IEnumerator FiniteState()
{
while(alive)
{
switch(state)
{
case State.PATROL:
Patrol();
break;
case State.CHASE:
Chase();
break;
}
yield return null;
}
}
void Patrol()
{
agent.speed = patrolSpeed;
if(Vector3.Distance(this.transform.position, markers[markerID].transform.position) >= 2)
{
agent.setDestionation(markers[markerID].transform.position);
character.move(agent.desiredVelocity, false, false);
}
else if(Vector3.Distance(this.transform.position, markers[markerID].transform.position) <= 2)
{
markerID += 1;
if(markerID>markers.Length)
{
markerID = 0;
}
}
else
{
character.move(Vector3.zero, false, false);
}
}
void Chase()
{
agent.speed = chaseSpeed;
agent.setDestionation(target.transform.position);
character.move(agent.desiredVelocity, false, false);
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
state = basicAI.State.CHASE;
target = other.GameObject;
}
}
}
}

Character does not "see" the player (3D Game)

I am following this unity 3D course. I followed every single step of the part called "Enemies Part 1: Static Observers", and after re-checking the code and doing researches for a day, I still did not find the problem. The scope of this part of the tutorial is to make that when the "Gargoyle" sees the player, when passing in front of him, should restart the game.
These are the two scripts that should make this work, but don't.
Observer (Gargoyle):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Observer : MonoBehaviour
{
public Transform player;
public GameEnding gameEnding;
bool m_IsPlayerInRange;
void OnTriggerEvent(Collider other)
{
if (other.transform == player)
{
m_IsPlayerInRange = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.transform == player)
{
m_IsPlayerInRange = false;
}
}
void Update()
{
if (m_IsPlayerInRange)
{
Vector3 direction = player.position - transform.position + Vector3.up;
Ray ray = new Ray(transform.position, direction);
RaycastHit raycastHit;
if (Physics.Raycast(ray, out raycastHit))
{
if (raycastHit.collider.transform == player)
{
gameEnding.CaughtPlayer();
}
}
}
}
}
And this is the GameEnding script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameEnding : MonoBehaviour
{
public float fadeDuration = 1f;
public float displayImageDuration = 1f;
public GameObject player;
public CanvasGroup exitBackgroundImageCanvasGroup;
public CanvasGroup caughtBackgroundImageCanvasGroup;
bool m_IsPlayerAtExit;
bool m_IsPlayerCaught;
float m_Timer;
void OnTriggerEnter(Collider other)
{
if (other.gameObject == player)
{
m_IsPlayerAtExit = true;
}
}
public void CaughtPlayer()
{
m_IsPlayerCaught = true;
}
void Update()
{
if (m_IsPlayerAtExit)
{
EndLevel(exitBackgroundImageCanvasGroup, false);
}
else if (m_IsPlayerCaught)
{
EndLevel(caughtBackgroundImageCanvasGroup, true);
}
}
void EndLevel(CanvasGroup imageCanvasGroup, bool doRestart)
{
m_Timer += Time.deltaTime;
imageCanvasGroup.alpha = m_Timer / fadeDuration;
if (m_Timer > fadeDuration + displayImageDuration)
{
if (doRestart)
{
SceneManager.LoadScene(0);
}
else
{
Application.Quit();
}
}
}
}
Back to the unity editor, I set the variables (player, gameending, exitimagebackground and caught imagebackground.
Does anybody know what the problem is and could help me out?
Thank you!
Edit:
these are the components of the Player Character:
and these of the Gargoyle:
Which has these as children:
which have these other components:
On your Observer class,
void OnTriggerEvent(Collider other)
{
if (other.transform == player)
{
m_IsPlayerInRange = true;
}
}
The function name is OnTriggerEvent(Collider), it should be OnTrigger**Enter**(Collider) instead.
Otherwise, it should work as intended.

How to make an object slide left and right automaticly in Unity 5.6? [duplicate]

This question already has an answer here:
Move GameObject back and forth
(1 answer)
Closed 5 years ago.
I'm currently developing a game in Unity 3D with c#. I developed some levels, and now I want to make some levels with auto moving cubes(the thematic of the game is cubes). I searched a lot on the internet but I don't find nothing wich satisfy me. Can someone help me? I really need help. Sorry if there are some grammare errors.
Create a new Script an name it SimpleTranslator.cs then copy and paste the below code.
using UnityEngine;
namespace TransformUtility
{
public class SimpleTranslator : MonoBehaviour
{
[Tooltip("The local target position towards we translate this gameObject. A red line is drawn.")]
public Vector3 m_localTargetPosition = new Vector3(0, 0, 5);
public float speed = 1;
public bool pingPong;
public bool translateOnAwake = true;
public new AudioSource audio;
Vector3 m_initialPosition, m_targetPosition;
Transform m_transform;
void Awake()
{
m_transform = transform;
m_initialPosition = m_transform.position;
SetTargetPosition(m_localTargetPosition);
enabled = translateOnAwake;
}
void FixedUpdate()
{
if (audio && !audio.isPlaying)
audio.Play();
m_transform.position = Vector3.MoveTowards(m_transform.position, m_targetPosition, speed * Time.deltaTime);
if (m_transform.position == m_targetPosition)
{
if (pingPong)
{
SwitchDirection();
}
else
{
enabled = false;
if (audio)
audio.Stop();
}
}
}
public bool isTranslating
{
get
{
return enabled;
}
}
public void SwitchDirection()
{
enabled = true;
if (m_transform.position == m_initialPosition)
{
SetTargetPosition(m_localTargetPosition);
}
else
{
m_targetPosition = m_initialPosition;
}
}
public void MoveToTargetPosition()
{
enabled = true;
SetTargetPosition(m_localTargetPosition);
}
public void MoveToInitialPosition()
{
m_targetPosition = m_initialPosition;
enabled = true;
}
public bool isInInitialPosition
{
get
{
return m_transform.position == m_initialPosition;
}
}
public bool isInTargetPosition
{
get
{
return m_transform.position == m_initialPosition + m_transform.TransformDirection(m_localTargetPosition);
}
}
private void SetTargetPosition(Vector3 localPosition)
{
m_targetPosition = m_initialPosition + transform.TransformDirection(localPosition);
#if UNITY_EDITOR
m_endPositionDebug = m_targetPosition;
#endif
}
#if UNITY_EDITOR
Vector3 m_endPositionDebug;
void OnDrawGizmos()
{
if (!Application.isPlaying)
{
Debug.DrawRay(transform.position, transform.TransformDirection(m_localTargetPosition), Color.red);
}
else
{
Debug.DrawLine(m_initialPosition, m_endPositionDebug, Color.red);
}
}
#endif
}
}

Categories