I am using Unity 2020.1.15f1.
First of all if you don't know you can right click on unity editor on a transform or rect transform select "Copy World Placement" at the bottom and get this info.
UnityEditor.TransformWorldPlacementJSON:{"position":{"x":-17.771259307861329,"y":-9.999999046325684,"z":90.0},"rotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"scale":{"x":1.0,"y":1.0,"z":1.0}}
This is exactly what i need in script but could not find a way to get this info from rect transform components. It does not have to be in JSON format all i need is THE SAME x and y values.
PS: transform.position is not what i want. It does not give these values for a rect transform.
Edit:The reason I could not get these values in script from position was the project was taking these values in void Awake() method which changes position values you get if you call it in void Start()
So i guess derHugos first answer is correct for this question.
For me (Unity 2020.2.5f1) this simply returns the transform.position, transform.rotation and transform.localScale regardless of whether it is a RectTransform or just a Transform.
Note though that a RectTransform in a Canvas of type Screenspace - Overlay is in pixel space!
I don't know how it works internally but basically you could do something like
public static class TransformExtensions
{
[Serializable]
private struct TransformData
{
public Vector3 position;
public Quaternion rotation;
public Vector3 scale;
public TransformData(Transform transform)
{
position = transform.position;
rotation = transform.rotation;
scale = transform.localScale;
}
}
public static string CopyWorldPlacementJson(this Transform transform, bool humanReadable = false)
{
var data = new TransformData(transform);
var json = JsonUtility.ToJson(data, humanReadable);
return json;
}
}
With that extension method anywhere in your project you can simply do
var json = someGameObject.transform.CopyWorldPlacementJson();
Debug.Log(json);
In particular to UI stuff you might have to wait for the next re-draw of the rects. You could force this using Canvas.ForceUpdateCanvases
Force all canvases to update their content.
A canvas performs its layout and content generation calculations at the end of a frame, just before rendering, in order to ensure that it's based on all the latest changes that may have happened during that frame. This means that in the Start callback and the first Update callback, the layout and content under the canvas may not be up-to-date.
Code that relies on up-to-date layout or content can call this method to ensure it before executing code that relies on it.
The reason I could not get these values in script from position was the project was taking these values in void Awake() method which changes position values you get if you call it in void Start()
So i guess derHugos first answer is correct for this question.
Related
I am working on a project using Unity2D, the goal is to be able to reference other main scripts to gain information from. In this specific case, I need to detect if the mouse pointer is touching a collider, however, from a separate script.
Usually, I would be able to create a boolean, and on mouse over set it to true, on mouse exit set it to false, like this:
bool isHovered;
void OnMouseEnter() {
isHovered = true;
}
void OnMouseExit() {
isHovered = false;
}
However, in the script, instead of doing this for each individual script, I would like to reference another script, like this:
public GameManager g;
void Update() {
if (g.IsTouchingMouse(gameObject)) { //Code }
}
But there's multiple problems with this. In my game manager class, I would need something like this
public bool IsTouchingMouse(gameObject g) { return value }
Which there is multiple issues with this, because I don't have a way to register the OnMouseEnter and OnMouseExit events for those objects on another script, and I don't have a way to store the values for every single gameObject to ensure this will work for every object without having to manually modify this script.
I'm looking for two things, #1, how can I detect mouseovers on objects from scripts who's parents are not that gameObject, two, are there any ideas on how I would go about creating a function to return this variable instantly?
Somewhat annoying, but I figured out a solution a few minutes after posting. So I will share it here.
public bool IsTouchingMouse(GameObject g)
{
Vector2 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
return g.GetComponent<Collider2D>().OverlapPoint(point);
}
What this code is basically doing, is making a function that takes a gameObject as an input, creates a vector2 based on the position of the mouse cursor in world space, and then returns weather or not the 2D collider that the object contains is actually touching this imaginary point, the variable "point" should be interchangable with any 2D world space location. I was pretty much overcomplicating the entire issue.
Find two resources you like and import them into Unity's Assets and set their Texture Type to Cursor
Create a new script and mount it to an empty object.
Open the script, create three public variables of type Texture2D, return to Unity and drag your favorite pointer map to the variable window.
To set the object label, we judge which pointer to change by customizing the label; so first set the label for the object. For example, the ground, I added the Ground tag to it, the wall added the Wall tag; the column added the Cylinder tag.
Write a method to change the mouse pointer, where the main API for changing the pointer is Cursor.SetCursor()
void setCursorTexture()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//Define the ray pointed by the mouse in the game window
RaycastHit hitInfo; //Information of ray collision
if (Physics.Raycast(ray, out hitInfo))//Determine whether to hit the object
{
// switch pointer
switch (hitInfo.collider.gameObject.tag)
{
case "Ground":
Cursor.SetCursor(groundCr, new Vector2(16, 16), CursorMode.Auto);
break;
case "Wall":
Cursor.SetCursor(wallCr, new Vector2(16, 16), CursorMode.Auto);
break;
case "Cylinder":
Cursor.SetCursor(CylinderCr, new Vector2(16, 16), CursorMode.Auto);
break;
default:
Cursor.SetCursor(null,Vector2.zero,CursorMode.Auto);
break;
}
}
}
Implement this method in the Update() method called every frame.
END (you can go to run the program) Thank you and hope to help you
This method works without exception. To solve the problem try the following method:
public Collider2D collider; // target Collider
void Update()
{
var mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (collider.bounds.IntersectRay(mouseRay)) Debug.Log("Mouse on collider..");
}
For New Input System?
But Input.mousePosition gives a new system error in Unity. To solve this problem, call the mouse position as shown below:
var mousePos = Mouse.current.position.ReadValue();
You can also check if (new input system enabled) is active as below:
#if ENABLE_INPUT_SYSTEM
var mousePos = Mouse.current.position.ReadValue();
#else
var mousePos = Input.mousePosition;
#endif
then follow the direction of the camera ray:
var mouseRay = Camera.main.ScreenPointToRay(mousePos);
I've been trying to fix this one bug in my code for over 7 hours now, upon being teleported, the movement controls cease to function, the mouse works fine, you can look around, but you can't move around.
I wanted to set up some simple code that would teleport the player to a "checkpoint" upon achieving a negative or null y level. I was doing this for a parkour based game, if the player fell off the platform they would have to start over, but after teleporting, it becomes impossible to move as I'm sure I have already said. My code is pretty simple:
public class Main : MonoBehaviour
{
float Fall;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Vector3 Checkpoint = new Vector3 (0,3,0);
GameObject Player = GameObject.FindGameObjectWithTag("Player");
Fall = GameObject.FindGameObjectWithTag("Player").transform.position.y;
if (Fall<-4)
{
Player.transform.position = Checkpoint;
}
}
}
You would think that this would just simply change the coordinates of the player, but I think this might be screwing with the FPSController script.
I am using Unity3d, with Standard Assets imported, All of the code is in C#.
Instead of checking the Y value of your character, I would instead place a death collider under the map. Make this a trigger and if the player touches this trigger, then teleport them back. Nothing with your code should screw with the FPS controller so it might be something else. I would also highly recommend not using a FindGameObjectWithTag in the Update() method as it is extremely expensive to use this every frame, especially twice. If you would rather keep the Update() Y component of the position check, please rewrite the code to something like this:
public class Main : MonoBehaviour
{
// assign this object of your player in the inspector - it stores the reference to reuse
// instead of grabbing it every frame
[SerializeField] private Transform playerTransform = null;
// make this a variable as it is not changing - might as well make this const too
private Vector3 checkpoint = new Vector3(0, 3, 0);
// constant value of what to check for in the Y
private const int FALL_Y_MARKER = -4;
// Update is called once per frame
void Update()
{
if (playerTransform.position.y < FALL_Y_MARKER)
{
playerTransform.position = checkpoint;
}
}
}
With your current code, there should be nothing breaking your input/movement, but with that said, we can not see your input/movement code. All the above snippet does is check if the Y component of the player objects position is below a certain value, and if it is, it sets the position to a new vector. Can you post a bit more movement code or somewhere else it can go wrong that you think is the issue?
I have a large sprite on my screen but I want the image it displays to scroll infinitely horizontally.
I have the current code which does not have any effect at all.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjLayer : MonoBehaviour
{
public float Speed = 0;
public int layer = 0;
protected Material _material;
protected float currentscroll = 0f;
// Start is called before the first frame update
void Start()
{
_material = GetComponent<SpriteRenderer>().material;
}
// Update is called once per frame
void Update()
{
currentscroll += Speed * Time.deltaTime;
var currentOffset=_material.GetTextureOffset("Layer3");
_material.SetTextureOffset("Layer3", new Vector2(currentscroll, 0));
_material.mainTextureOffset = new Vector2(currentscroll, 0);
}
}
Just to note that I am setting both SetTextureOffset and mainTextureOffset as neither seem to be working.
Also currentOffset is changing as expected but the texture is not moving on the screen.
You are most likely using a Material that doesn't support texture offsetting, unless the property is HiddenInInspector you can check in the inspector if your material supports it by checking if it has the Offset x,y input fields underneath "Texture"
The standard Unlit/Texture shader has this property, so do any newly created UnlitShaders (as seen in my example screenshot).
If you are using a custom shader then your Texture isn't named "_MainTex", which is the property that Unity looks for when using mainTextureOffset, as cited from the docs:
By default, Unity considers a texture with the property name name "_MainTex" to be the main texture.
Using material.mainTextureOffset works fine for me when using a shader where the Texture is called _MainTex:
public Vector2 offset;
private Material material;
private void Start()
{
material = GetComponent<MeshRenderer>().material;
}
private void Update()
{
material.mainTextureOffset = offset;
}
Result (Gyazo gif)
In your example
_material.SetTextureOffset("Layer3", new Vector2(currentscroll, 0));
You are looking for a property named "Layer3" in your shader, this is not a name that is standard in use by Unity (in my knowledge), if you are using a custom shader then make sure your Texture property has the name Layer3 inside your shader.
Update after OP's comment:
Tiling and Offsetting is not available to Materials on a SpriteRenderer, Sprites are assumed to not be offset. As indicted by the warning given by Unity when you try to add a material that has Tiling/Ofsetting in its texture
Material texture property _MainTex has offset/scale set. it is incompatible with spriterenderer
Instead use a Quad, Plane, Image or raw Image component which does have support for materials with tiling/offsetting in combination with the above code.
I think technically it is still possible to use offsetting with sprites by throwing on a shader that support it and ignoring the warning, but I can not guarantee this won't break down the line or what the performance implications are
I have an enemy that has children in it; the enemy also has a death animation. Within the death animation (using the animator), I have scaled the enemy to an appropriate size. However, the children within the enemy is also being scaled down even though I have a animation on the child where I have sized it, I also added anchor positions on this child. Is there a way I can scale down the enemy but also keep the size of the child, p.s. the child is a UI text object. Thank you!
The easiest way to solve your problem that I see is to introduce another GameObject higher in the hierarchy.
This GameObject would be the parent of your enemy object and your Text object which currently is a child of the enemy.
This way you can scale the enemy independently of the Text.
Maybe (hopefully) there are better solutions but you could use a component on the child objects that always keeps the original scale inverting relative changes in the parents scale
public class FreezeScale : MonoBehaviour
{
private Vector3 originalScale;
private Vector3 parentOriginalScale;
private void Awake()
{
// afaik RectTransform inherits from Transform
// so this should also work for UI objects.
originalScale = transform.localScale;
parentOriginalScale = transform.parent.localScale;
}
private void LateUpdate()
{
var currentParentScale = Transform.parent.localScale;
// Get the relative difference to the original scale
var diffX = currentParentScale.x / parentOriginalScale.x;
var diffY = currentParentScale.y / parentOriginalScale.y;
var diffZ = currentParentScale.z / parentOriginalScale.z;
// This inverts the scale differences
var diffVector = new Vector3 (1/diffX, 1/diffY, 1/diffZ);
// Apply the inverted differences to the original scale
transform.localScale = originalScale * diffVector;
}
}
Not tested since hacked in on my mobile phone but I hope you get the idea ;)
set the child's scale to world space not local space.
local space is the default, but it will go off of the scale of the parent so when the enemy shrinks so will the text.
alternatively you could set both objects to be children of an empty object, then just scale your enemy down and the text should stay the same size since its using the scale of the empty parent, which isn't changing size either.
see here:
public static Vector3 GetWorldScale(Transform transform)
{
Vector3 worldScale = transform.localScale;
Transform parent = transform.parent;
while (parent != null)
{
worldScale = Vector3.Scale(worldScale,parent.localScale);
parent = parent.parent;
}
return worldScale;
}
just a work around though, your meant to use this:
yourtransform.LocalScale=Transform.localToWorldMatrix
but it gives me issues... the above method works well though.
transform.scale=GetWorldScale(transform);
edit: lets be clear, the easiest thing to do would be to unpraent the objext before shrinking the parent. this will separate the scales.
I am spawning a prefab object at runtime (actually, in the Start() method of another object), and I need to apply a scaling to the object. I made a little component to handle this:
public class Spawner : MonoBehaviour {
public Transform SpawnPrefab;
public Vector3 Scale;
void Start () {
var spawn = Instantiate(SpawnPrefab, Vector3.zero, Quaternion.identity);
spawn.localScale = Vector3.Scale(spawn.localScale, Scale);
// spawn.GetComponent<Rigidbody>().ResetCenterOfMass(); // Has no effect
}
}
The pivot point of the prefab I am spawning does not coincide with the centre of mass of the object. Therefore, the rescaling means that the centre of mass location relative to the pivot will change. However, it's not being updated automatically, so my spawned object has unexpected physics.
I tried adding a call to GetComponent<Rigidbody>().ResetCenterOfMass() immediately after the call to Scale() (the commented-out line above), but this has no effect.
However, if I put the call to ResetCenterOfMass() in the Start() method of a separate little component added to the spawned object, e.g.
public class COMReset : MonoBehaviour {
void Start() {
GetComponent<Rigidbody>().ResetCenterOfMass();
}
}
this does cause the centre of mass to be recalculated correctly. However, the spawned object appears to have already been through at least one physics update with the wrong COM by this time, and so has already acquired some unexpected momentum.
Why isn't the COM being automatically recalculated, without me having to call ResetCenterOfMass() explicitly? And if I must trigger it manually, can I do that immediately after the calls to Instantiate() and Scale(), rather than deferring like this?
With thanks to #DMGregory on GameDev for the suggestion, a call to Physics.SyncTransforms before invoking Rigidbody.ResetCenterOfMass fixes the problem:
public class Spawner : MonoBehaviour {
public Transform SpawnPrefab;
public Vector3 Scale;
void Start () {
var spawn = Instantiate(SpawnPrefab, Vector3.zero, Quaternion.identity);
spawn.localScale = Vector3.Scale(spawn.localScale, Scale);
Physics.SyncTransforms();
spawn.GetComponent<Rigidbody>().ResetCenterOfMass();
}
}
Evidently this direct modification of the transform scale isn't being automatically passed through to the physics engine, but Physics.SyncTransforms lets us manually flush those changes down to PhysX, so that the ResetCenterOfMass computation is then based on the correctly scaled transform.