Hey guys I've been working on a 2D terraria-like game, from a tutorial I found on Youtube. I've been using Quads as blocks. The link to the series is:
https://www.youtube.com/watch?v=KONw5GX0Ixs
The only difference is I've been in a 2D project rather than a 3D. I'm trying to implement a mining system, but the clicks aren't detected much less the blocks that I'm clicking on. The system doesn't even detect the clicking, much less the blocks I'm clicking on. I'm new to coding so I'm not sure how to fix this, anything helps, thanks.
My code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mining : MonoBehaviour {
public Vector2 direction;
void Update() {
if (Input.GetMouseButtonDown (0)) {
Vector3 c = Camera.main.ScreenToWorldPoint (Input.mousePosition);
RaycastHit2D hit2D = Physics2D.Raycast(this.gameObject.transform.position, direction);
Debug.Log ("hello");
if (hit2D.collider.gameObject != null) {
Debug.Log (hit2D.collider.gameObject);
Destroy (hit2D.collider.gameObject);
}
}
}
}
First of all, change
Input.GetMouseButtonDown(0)
to
Input.GetMouseButton(0)
as its
Returns whether the given mouse button is held down.
tell me if that fix your problem. If not, I will try to figure sth out:)
Related
I've been studying c sharp as part of my games course and my teacher has left and they cant find a replacement so I'm really struggling with coding my demo game. I’ve got a problem with my doors in my game and I was wondering if anyone knew how u can get it to work because I haven’t really got a clue when it comes to coding. Basically what I’ve got is my doors are shut then when I start my game they open and then shut but I need a code that will make sure it says shut until I’m in the trigger point but all the YouTube clips haven't been working and it says it’s got problems with it. I know what I want it to do I just don’t know how to put that into action.
This is my code but I don't think it does anything:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorAnimation: MonoBehaviour
{
Animator animator;
int isOpeningHash;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
animator = GetComponent<Animator>();
isOpeningHash = Animator.StringToHash("isOpening");
bool isOpening = animator.GetBool(isOpeningHash);
bool OpenPressed = Input.GetKey("e");
bool CloseDoor = Input.GetKey("left shift");
if (!isOpening && OpenPressed)
{
animator.SetBool(isOpeningHash, true);
}
if (isOpening && OpenPressed)
{
animator.SetBool(isOpeningHash, false);
}
}
}
First of all, make 2 animations(and turn off the loop or it will loop for eternity) for the opening and closing of door. Don't make Bools for the animator. It's basically useless. At the top of a script declare a bool called as open and set it equal to false(if the door is closed from start). Check if E is pressed and open is set to false(the door is closed), play the opening animation, set open to true and if left shift is pressed and open is set to true(the door is open) play the closing animation, set open to false. You can play animations if a certain key is pressed like this https://answers.unity.com/questions/1362883/how-to-make-an-animation-play-on-keypress-unity-ga.html
This should do your job :)
I would like to know if it is possible to trigger a script at a specific time in the Unity Timeline.
I don't want it to be triggered for the duration of the application, but only after a certain time.
Indeed, with this script the user will be able to type on the touch screen as soon as the script is triggered.
My problem is that this script is launched from the start of the application!
Should we handle this directly in the Timeline or should I modify my script?
Thank you for your precious help
using UnityEngine;
using System.Collections;
public class SelectObject : MonoBehaviour
{
void Update()
{
if (Input.GetKeyDown("escape"))
Application.Quit();
TapSelect();
}
void TapSelect()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Application.OpenURL("https://www.");
}
}
}
}
}
What you need depends on your UnityVersion, from 2019.1 and above UnityTimeline introduces Signals, which is what you need (Unity has his own videotutorial teaching how they work).
But if you are using an older version of Unity (<2019.1) you need to write your own CustomPlayables (there are some DefaultPlayables to check on the assetstore, and this tutorial really helps me out to understand how they work).
Other good Timeline reference: https://blogs.unity3d.com/es/2018/09/05/extending-timeline-a-practical-guide/
Okay, so, I got the challenge to create a program, in Unity for GearVR.
I have to make a program which makes use of gaze input, so if you stare at an object for several seconds it'll display you a 360* video.
I barely can't find any GearVR Gaze Input tutorials around on the web so I wanted to give it a shot on Stackoverflow, and hopefully someone could help me out! :)
You have to use Physics.Raycast. This method emits a ray from the camera point to the camera orientation. You can use something like that:
// Does the Ray hit an object with a component named MyObjectScript?
RaycastHit hit;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, out hit) )
{
var script = hit.transform.GetComponent<MyObjectScript>();
if (script != null)
{
//Do your stuff...
}
}
Simply put this script on your camera in the FixedUpdate method and another script named MyObjectScript in the object you want to detect.
I have a simple FPS where a player can fire a gun. I want the clients to see the bullet holes. I am trying to accomplish this by invoking a server side method when the client tries to fire the gun. The server side method should then raycast from the players camera and spawn a bullet hole for everyone to see..
I can get this to work partially by passing the player as a parameter to the server method. However, since my raycasting needs to be done based off the player camera, the bullet holes end up appearing at character height, since it's using the player and not the camera.
It won't allow me to pass a camera through. I have also tried creating an empty game object called bullet spawn and passing that through but I was getting a "object reference not set to instance of an object." This same error seems to surface for any Child element of the player I attempt to pass to the server side method.
I am unsure of the right way to accomplish this.
void Update()
{
if (isLocalPlayer)
{
if (Input.GetButton("Fire1"))
{
CmdFire(BulletSpawn);
}
}
}
[Command]
void CmdFire(GameObject Player)
{
Ray shooterRay = new Ray(Player.transform.position, Player.transform.forward);
if (Physics.Raycast(shooterRay, out Hit, 10000))
{
Debug.Log("player hit");
GameObject Bullet_Hole = (GameObject)Instantiate(BulletHole_Prefab, Hit.point, Quaternion.FromToRotation(Vector3.up, Hit.point));
NetworkServer.Spawn(Bullet_Hole);
}
}
Think about it this way. What is it that you need to fire your weapon? You need the firing point and the firing direction (in your simple case). So try to pass just that information. Your CmdFire should look like void CmdFire(Vector3 firingPoint, Vector3 firingDirection). Remember that when you pass the player gameobject, it gets serialized and sent to the server which causes a lot of unneeded network traffic. Stick to value types and simple objects for commands.
This is what ultimately led me to the solution. Thank you for the advice. I simply passed two Vector3 parameters as you suggested which represent the raycast position and direction. I can now shoot from both client and server and see the bullet holes on both as well. Here is the code that fixed it in case anyone runs into this later on:
void Update() {
if (isLocalPlayer) {
if (Input.GetButton("Fire1")) {
Vector3 FiringSpot = PlayerCam.transform.position;
Vector3 FireDirection = PlayerCam.transform.forward;
CmdFire(FiringSpot, FireDirection);
}
}
}
[Command]
void CmdFire(Vector3 firingPoint, Vector3 firingDirection) {
Ray shooterRay = new Ray(firingPoint, firingDirection);
if (Physics.Raycast(shooterRay, out Hit, 10000)) {
GameObject Bullet_Hole = (GameObject)Instantiate(bullet_Hole_Prefab, Hit.point, Quaternion.FromToRotation(Vector3.up, Hit.point));
NetworkServer.Spawn(Bullet_Hole);
Destroy(Bullet_Hole, 10);
}
}
So, I'm doing an FPS game in Unity5, and some days ago I started looking at multiplayer tutorials at the official site of Unity.
However, I changed some bit of the code to fit in an FPS, but I had a problem when started doing this. The client's camera moved fine, but the host's one did not: It used the client's camera to see, but his player to move, sort of a third-person view.
All players have the same code, and the camera has a Network Identity with Local Player Authority turned on. Also, it has this C# script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class VisionControl : NetworkBehaviour {
void Start () {
Cursor.lockState = CursorLockMode.Locked;
}
void Update () {
if(!isLocalPlayer){
return;
}
transform.Rotate (new Vector3 (-Input.GetAxis ("Mouse Y")*5.0f,0.0f,0.0f));
}
}
Your camera should not have a network identiy.
It is used only by the local clients to get a perspective of the game world, so they do not need to be networked.
Each camera exists on the current client.for the current client only.
Each game instance should have 1 camera running, that of the Local Player.
Attach a simple non-networked camera component to your player prefab and test again.
Fixed! I removed the VisionControl script and putted this one on the player's movement script:
public override void OnStartLocalPlayer ()
{
if (isLocalPlayer) {
FindObjectOfType<Camera> ().gameObject.transform.position = this.transform.position+new Vector3(0f,0.5f,0f);
FindObjectOfType<Camera>().gameObject.transform.SetParent (this.transform);
}
}