I have made a basic scripted tile, it have set prefab as game object. Problem is, that gameobject is offset when door is placed. I would like to have gameobject on same location as tile.
IMAGE: My problem
DoorTile.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Tilemaps;
public class DoorTile : TileBase
{
public Sprite doorSprite;
public GameObject doorObject;
public Tile.ColliderType colliderType = Tile.ColliderType.Sprite;
public override void RefreshTile(Vector3Int location, ITilemap tilemap)
{
tilemap.RefreshTile(location);
}
public override void GetTileData(Vector3Int location, ITilemap tilemap, ref TileData tileData)
{
tileData.gameObject = doorObject;
tileData.colliderType = colliderType;
tileData.sprite = doorSprite;
tileData.flags = TileFlags.LockTransform;
tileData.color = Color.white;
}
}
This is due to your Tile Anchor being (0.5, 0.5, 0). You can modify your tile anchor under the Tilemap component to fix this, or you can simply add the offset to your Door object.
Related
I am trying to instantiate a prefab and I want it's location to be the exact same as the position of game object with name "spawnedPos" . Somehow the prefab is not instantiated on the exact same position.
code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShurikenSpawn : MonoBehaviour
{
public GameObject[] gameReference;
public GameObject spawnedShar;
public Vector2 pos;
public Transform playerPos;
public float posX, posY;
public Transform spawnedPos;
void Start()
{
}
private void Update()
{
spawnedPos = GameObject.FindWithTag("spawnedPos").transform;
playerPos = GameObject.FindWithTag("Player").transform;
//posX = playerPos.position.x;
//posY = playerPos.position.y;
// pos = new Vector2(posX, posY);
spawnedPos.position = playerPos.transform.position;
// spawnedPos.transform.position = pos;
if (Input.GetKeyDown(KeyCode.F))
{
spawnedShar = Instantiate(gameReference[0],spawnedPos);
}
}
}
The code you are using for instantiation sets the parent of the object and not the position.
Use this:
Instantiate(gameReference[0],spawnedPos.position, Quaternion.identity ,spawnedPos);
I've recently discovered the URP and am developing a 2D game for fun. I added a global 2d light and set up some code to grab the light component, but unity keeps saying that light 2D does not exist. The error in unity is "error CS0246: The type or namespace name 'Light2D' could not be found". This is the code I have so far. I also followed a tutorial by Jimmy Vegas to grab the system time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class DayNight : MonoBehaviour
{
public GameObject theDisplay;
public GameObject GL;
public int hour;
public int minutes;
public int totaltime;
private bool AM;
void Start()
{
totaltime = System.DateTime.Now.Hour;
hour = System.DateTime.Now.Hour;
minutes = System.DateTime.Now.Minute;
}
public void Night()
{
//change light settings
GL.GetComponent<Light2D>().Intensity = 0.4;
}
}
To reference the Light2D Component you need to add the UnityEngine.Experimental.Rendering.Universal namespace to your code.
Namespace Example:
...
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;
public class DayNight : MonoBehaviour
{
...
}
If you get an Error you need to just continue and try to compile it, to see if it actually works.
After that is done you should be able to access the Light2D Component of your GameObject without any further troubles.
Calling Light2D Example:
public float nightvalue = 0.4f;
public void Night()
{
// Change light intensity to nightvalue.
GL.GetComponent<Light2D>().intensity = nightvalue;
}
I am new to Unity and as I am creating my interface, I am trying to find what's the distance between two game objects. I know how to do in a C#/javascript script, however I am strugling to find this information in the scene view. Any key I could press while moving an object to see the distance to its neighbours ?
In a script, you can use Vector3.Distance between vector3 to get the distance between two points. Every gameObject has a position that is represented in a Vector3.
Below is a script example that shows you how it works. You just have to drag the script onto a gameObject in your scene and drag in the inspector another gameobject in your scene. The script should run even when your not in play mode because of the [ExecuteInEditMode].This way you will see the distanceBetweenObjects update in real time without actually having to hit play.
using UnityEngine;
[ExecuteInEditMode]
public class DistanceBetweenTwoObjects : MonoBehaviour
{
public GameObject obj;
public float distanceBetweenObjects;
private void Update()
{
distanceBetweenObjects = Vector3.Distance(transform.position, obj.transform.position);
Debug.DrawLine(transform.position, obj.transform.position, Color.green);
}
private void OnDrawGizmos()
{
GUI.color = Color.black;
Handles.Label(transform.position - (transform.position -
obj.transform.position)/2, distanceBetweenObjects.ToString());
}
}
The method OnDrawGizmos will draw text in between the 2 objects showing the distance value to help make it more user friendly.
There isn't any built-in functionality for this, but it's fairly trivial to add such a readout into the display using [ExecuteinEditMode], which causes scripts to run even when the game is not in play mode. This script, for example, should readout the distances on different axes and in total:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class EditorDistanceDisplay : MonoBehaviour
{
public GameObject target1;
public GameObject target2;
public float distanceX;
public float distanceY;
public float distanceZ;
public float distanceTotal;
void Start()
{
}
void Update()
{
Vector3 delta = target2.transform.position - target1.transform.position;
distanceX = delta.x;
distanceY = delta.y;
distanceZ = delta.z;
distanceTotal = delta.magnitude;
}
}
in my Unity scene I'm trying to activate a portal animation when you pick up all the coins on that level, but the problem is that my portal script does not detect when the coin counter value is equal to 6 (that's the total number of coins in that level).
I have this script which is the script that is attached to the coins and increases the coin counter value.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MonedaScript2 : MonoBehaviour
{
public GameObject moneda;
public AudioClip clip;
public Vector3 PosicionMoneda;
public void Update ()
{
transform.Rotate (0, 90 * Time.deltaTime, 0);
}
public void OnTriggerEnter(Collider other)
{
if (other.name == "Player")
{
AudioSource.PlayClipAtPoint(clip, PosicionMoneda, 1f);
AparecerPortalNivel9.ContadorMonedas += 1;
Destroy(moneda);
}
}
}
And then i have the portal script which sould detect ewhen you pick all the coins. It is attached to the PanelPortal gameobject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AparecerPortalNivel9 : MonoBehaviour
{
public Animator anim;
public GameObject PanelPortal;
//public ScoringSystem SS;
public bool DeberiaAparecer = true;
public static int ContadorMonedas;
void Start()
{
//SS = GameObject.FindGameObjectWithTag("SistemaDeScore").GetComponent; ScoringSystem();
PanelPortal.gameObject.SetActive(false);
}
void Update()
{
if (ContadorMonedas == 6f)
{
Debug.Log("FuncionaHostia");
PanelPortal.gameObject.SetActive(true);
anim.SetBool("Aparecer",true);
DeberiaAparecer = false;
}
}
}
Could someone help me on what should i do so my portal script detects when the coins picked up are 6 and then run all the functions inside the IF method?
Thanks everyone
A MonoBehaviour's Update will never run while the gameobject it is on is disabled.
Because AparecerPortalNivel9 is attached to PanelPortal, when you do this you are preventing the Update method from being called:
PanelPortal.gameObject.SetActive(false);
The easiest solution here is to move AparecerPortalNivel9 to a different gameobject.
I am trying to change mass of a 3D object programmatically. But the object doesn't get the calculated mass but a 0 value initially. When the prefab of the object is created it gets the calculated mass of the previous object not the current mass. And the scenario repeats for all the prefabs created henceforth. How can I get around this problem? Any help is greatly appreciated.
You're probably missing a get component call:
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
public float mass;
public Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
rb.mass = 0.5f;
}
}
To give your rigidbody a mass can use the following script wise
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
void Example() {
rigidbody.mass = 0.5F;
}
}
Next time do some more research.
http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-mass.html
Google first hit.