How To Add Mobile Touch Controllers In Unity - c#

I am working on a game, and till now, i have allowed a user to play the game by moving up and down using the up and down arrow keys on the computer keyboard.
But now i want to make the user be able to do that on a mobile phone by touching up or down, or maybe swiping up or down to move in the game.
I am using c# for this.
This is the current code assigned to the player object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float playerSpeed;
private Rigidbody2D rb;
private Vector2 playerDirection;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float directionY = Input.GetAxisRaw("Vertical");
playerDirection = new Vector2(0, directionY).normalized;
}
void FixedUpdate()
{
rb.velocity = new Vector2(0, playerDirection.y * playerSpeed);
}
}
What do i need to do to achieve this?

You should try to make buttons with trasparency

You have plenty of possibilities here !
But there might have better solutions than others ;)
You could make a UI representing up and down arrow that respond with wathever behaviour you'd like when pressed ORyou could dig a little the Input documentation, espacially what's relative to touches.
From there you can get touch position in screen space an make whatever you want with it !
The choice is lead by the type of game or application you're trying to make. Hope that helped ;)

Related

I control the gameobject by tilting the phone and need to keep it balanced in whatever the rotation of the phone is when game starts

I published a game named Rotate Ball Pro last week. But some people told me that playing the game was not so comfortable.
I used unity and C# for coding. My game levels has labyrinths and a sphere on it. The sphere has rigidbody component but the labyrinths have not. So you can control the labyrinth by tilting the phone and thus the sphere can roll. But you must hold the phone parallel to the ground to keep the labyrinth balanced. For example you can not play by lying on back or holding the phone with any angle. So this makes playing uncomfortable.
I want to to keep the labyrinth balanced in whatever the rotation of the phone is when the game starts. I searched web and tried many things but could not solve it. Can anyone help me?
Here is the explanation of the problem with an image: Problem
Here is the direct game link: Rotate Ball Pro
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class NewMovementScript : MonoBehaviour
{
public float multiplier = 0;
void Start()
{
Input.gyro.enabled = true;
Screen.sleepTimeout = SleepTimeout.NeverSleep;
}
void FixedUpdate()
{
var dir = Vector3.zero;
dir.x = Input.acceleration.x * -1 * multiplier;
dir.y = Input.acceleration.z * multiplier;
dir.z = Input.acceleration.y * multiplier;
transform.eulerAngles = new Vector3(dir.z, 0f, dir.x);
}
}
Input.acceleration is the position change between the last and current frame and as you noted already doesn't take the initial state into account.
This also is barely related to rotations at all.
I think for your usage you would rather simply use Input.gyro.attitude!
Example from the docs
public class Example : MonoBehaviour
{
// Rotate the object to match the device's orientation
// in space.
void Update()
{
transform.rotation = Input.gyro.attitude;
}
}
If your object uses a rigidbody you might want to rather use
public class Example : MonoBehaviour
{
private Rigidbody rigidbody;
void Awake ()
{
rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
rigidbody.MoveRotation(Input.gyro.attitude);
}
}

How to make a open/close door with an action range?

I want to make a door which opens and closes by pressing a button ( "O" for open and "C" for close). Tried with this:
using System.Collections;
using UnityEngine;
public class DoorScript : MonoBehaviour {
public float speed;
public float angle;
public Vector3 direction;
// use this for initializaton
void start() {
angle = transform.eulerAngles.y;
}
// Update is called once per frame
void Update() {
if (Mathf.Round(transform.eulerAngles.y) != angle) {
//rotate our door
transform.Rotate(direction * speed);
}
if (Input.GetKeyDown(KeyCode.O)) {
angle = 90;
direction = Vector3.up;
}
if (Input.GetKeyDown(KeyCode.C)) {
angle = 0;
direction = -Vector3.up;
}
}
}
But it did not works how I want. I mean: You can be far away, if you press "O" the door will open anyway. How can I implement an action range? I mean, you will need to be a little bit closer to the door interact with it?
I also need to say that in the game will me aprox. 40 doors. I need for each other a custom script?
The code is in C#. I have a little bit of coding experience, but i canĀ“t solve this.
Thanks
One way to do this is to use Colliders, RigidBodies and OnTrigger calls. Here is a manual page that outlines the usage.
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
For your implementation, the door would have the Example script (call it something other than "Example") and the player would be the sphere (although you would create the player outside of this script). When the player is near a door, the door will know it by keeping track of the presence of the player through the OnTrigger calls. Then, when the key is pressed, the door will react if the user is nearby. Every door would have a script like this.

making a 2D GameObject react to the position of another 2D GameObject

So, my boss moves in a specific direction when it detects the player. The problem I'm having is how to get the boss to move depending on where the player is within a certain proximity. So if the boss is on the player's left, he'll move to the left. If he's on the player's right, he'll move to the right. But I can't figure out how to make him react based on distance. Right now I'm just doing a Debug.Log to save a few seconds.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class phantom : MonoBehaviour {
private Rigidbody2D rb;
private Animator anim;
public Transform Target;
void Start ()
{
rb = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}
void Update ()
{
if (transform.position.x > Target.position.x ) {
Debug.Log ("left");
}
if (transform.position.x < Target.position.x ) {
Debug.Log ("right");
}
}
}
You could use the Vector3.Distance method to determine the distance between your two object based on thier respective transform. That way, you can modify your boss's behavior according to his proximity to the player. The smaller the magnitude value is, the closer your two transforms are.
Ex:
int distanceYouWant;
if(Vector3.Distance(transform.position, Target.position).magnitude < distanceToDoStuff)
{
Debug.Log("Boss do stuff!");
}
Here is the link to the Unity scripting API doc: https://docs.unity3d.com/ScriptReference/Vector3.Distance.html
Hope this helps!
I figured it out. I just made the function not in Update but with an OnTriggerEnterStay2d (Collider2D other). Then I placed a trigger collider on the same GameObject and only when it detects the Target (the player) does the debug come up.

Why cant I move my camera in (Unity)C#?

camera transform
I am trying to move my camera based on the players' movements on Y axis in Unity.
However, it does not work...
What did I do wrong? I have attached image of my script (C#) here.
and, Yes, I did attach this script with Main Camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
GameObject player;
// Use this for initialization
void Start () {
this.player = GameObject.Find("cat");
}
// Update is called once per frame
void Update () {
Vector3 playerPos = this.player.transform.position;
transform.position = new Vector3(
transform.position.x, playerPos.y, transform.position.z);
}
}
Make the player GameObject public and just drag and drop your player in the inspector in unity see if that works? Are you getting any exceptions? Also add Debug.Log (player.transform.position.ToString ()) to see if it is showing the right values. Are you sure you player object name is cat and not Cat, it is case sensitive. Check on those things and let me know if you figured it out!

Unity C# - Rigidbody.AddForce not working as intended

So, I am trying to make a cube move with the WSAD buttons and for some reason, even though I am not touching any buttons, it moves diagonally up and to the left. I am using Unity 4.5.1
Code:
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float moveSpeed;
private Vector3 input;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
input = new Vector3(Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
print(input);
}
}
Your problem may be somewhere else, because the code seems just fine. If I were you, I'd try to take a look to this page. You might find the solution to this issue

Categories

Resources