Display Player Coordinates on Simple GUIText - unity3d

Novice here - of course. I'm using Unity 5, and am looking to display on screen the coordinates of a player using a FPS camera. The main reason is I'm using a camera path animator to fly around a terrain scene I built for class, and think by logging coordinates via walking around in FP will make placing my camera path waypoints a lot easier.
What I have done:
-Created new empty game object -Added Component -> Rendering -> GUIText -Added Component -> New Script ->C#
Now I of course searched around to find a solution and found this:
function OnGUI () {
GUI.Label (Rect (10,120,500,100), "X = " + transform.position.x + " Y= " + transform.position.y + "Z= " + transform.position.z);
}
Which is great! But I am not a strong C# programmer by any means, but I do know this needs a Player variable of some sort yet. So please, could anyone simply direct me, or mind posting the code needed to make player coordinates appear on the screen. This doesn't have to be pretty or anything, I just need to write down the data as I walk around in FP. (unless you want brownie points and know of a way to log coordinates into a .txt file on button press... hahaha Thanks guys!

I'm not sure this is what you want....
Create new GameObject(GUIText) and make it's parent with your player game object. Now GUIText game object will follow your player object automatically
Add new script(i.e, PlayerPositionLogger.cs). to GUIText game object.
Find player script on void Start() on PlayerPositionLogger.cs or declare public Player player; and link player object on editor.
Update content of GUIText with player object.
gist.github.com/growingdever/39b758d5f3967c143744

Select the player object
Add component > New Script
Add the following code to the script (replace Start and Update)
void OnGUI() {
GUI.Box (Rect (10,10,100,90), "Player position: X:" + transform.position.x +
" Y: " + transform.position.y + " Z: " + transform.position.z);
}
When you enter playmode it should do what you want. Your main problem was GUI layout.

Related

İn Unity Photon Network , Learn who did the bullet come from?

I make a game using photon network . 2 actors are shooting at each other and when the bullet is formed on the stage I want to know who the bullet came from. I can send player id in bullet Instantiate and I can find player in for loop but i don't think it's true.
Is there better than this method?
Code
void Shoot()
{
var part = GetComponentInChildren<ParticleSystem>();
part.Play();
float angle = cc.isFacingRight ? 0f : 180f;
GameObject gameob = PhotonNetwork.Instantiate("Bullet", firingPoint.position, Quaternion.Euler(new Vector3(0f, 0f, angle)),0, null);
}
You can simply use photonView.Owner, since the bullet is a networked object. Alternatively, you can use photonView.IsMine to check if the client running the check owns the bullet, instead of comparing owners.
(The photonView instance comes from assuming your bullet script extends MonoBehaviourPun instead of MonoBehaviour)

Unity trigger not triggering

I'm having one of the most basic issue but I really can't get it work on my project. I even tried to re-create a blank project and create a similar situation, in this new project, the trigger is working well but not in my old one and I can't figure out why so I'm looking for help here.
Here is some explanation on what I have, first, the character :
My character with :
Box Collider 2D with NOTHING check (so isTrigger, used by effector, etc, they are NOT checked)
Rigidbody 2D Dynamic, simulated, continuous and stat awake.
Then, the platform prefab :
Platform prefab (BoxCollider2D with Used By Effector, Platform Effector 2D)
Child of platform prefab > front part (just a sprite renderer)
Child of platform prefab > triggerPart (Empty object scale 4,1,1 with BoxCollider2D with is Trigger checked and the script "triggerBounce"
Here is the script triggerBounce : (For now, I'm just trying to see if the trigger is working)
private void OnTriggerEnter2D(Collider2D collision)
{Debug.Log("triger in " + name + " " + collision.name);}
private void OnTriggerStay2D(Collider2D collision)
{Debug.Log("triger stay " + name + " " + collision.name);}
private void OnTriggerExit2D(Collider2D collision)
{Debug.Log("triger exit " + name + " " + collision.name);}
I searched a lot of help on the internet before posting this, like this :
Putting all the element on the same Z position to be sure they're triggering
Reproducing the most simple situation, which I made work in an other project, but not in this one
I checked that in my project settings > physics 2D the Y gravity was -9.81
I tried resizing the boxCollider2D size
Verified the script triggerBounce was launched by debug.log("start") in his start
And even tough I did all this, it doesn't work any better.
Here is a pic of the situation, it can help you understand the problem :
Thanks to #Morion, I found what the problem was.
All my GameObject had the "default" layer and the collision matrix wasn't checked for 2 default.
So, I went to Edit > Project Settings > Physics 2D
and check the box where the row and column was default.

How to move an object in Unity

Hello guys I try to make a city building game the idea is very simple actually Instantiate an Building and move them with mouse after that hit the place button and place.
The issue is if the collider of a another building completely encloses the collider of the building that I am built I can't move new building.
I can probably explain better with pictures.
Issue 1
The first picture my new building you can see collider limits and the second one my old building that I already placed. I Understand the problem but I can not solve it.
And here it is my object drag code
private void OnMouseDrag()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000f,(1 << LayerMask.NameToLayer("Ground"))))
{
Debug.Log(hitInfo.transform.name);
transform.position = SnapToGrid(hitInfo.point);
}
}
private Vector3Int SnapToGrid(Vector3 pos)
{
Vector3 tempPos = pos;
Vector3Int snappedPos;
snappedPos = new Vector3Int(Mathf.RoundToInt(tempPos.x), Mathf.RoundToInt(tempPos.y), Mathf.RoundToInt(tempPos.z));
return snappedPos;
}
Thanks for the help in advance
If I got your question right, I believe that you can solve the problem by enableing the "Is Trigger" option of the previously placed buildings colliders at the moment that a new building is being moved around. Enableing this option in an object makes it so other objects can pass trought it when collision happens.

Updating values in a component in instantiated gameobject in unity

Hi i have a Robot Prefab, with a NPC script that is linked to dialogue sentences.
I am retrieving the information about dialogues (using a loader script) from a server API, and then instantiating both the robot gameobject and the sentences. For example for instantiating the gameobject:
GameObject newObject = GameObject.Instantiate(Resources.Load("prefabs/" + item.gameObject.name) as GameObject, parentProps);
newObject.transform.localPosition = new Vector3(loaded.x, loaded.y, loaded.z);
newObject.transform.localEulerAngles = new Vector3(0, 90f, 0);
However, i do not know how to access the dialogue and update it. Could someone tell me how to update the instantiated value of the sentences here (marked it with red arrows on the image below)?
May be a basic question, but am new. Thanks!
You can create a public function in Npc class and call it after you instantiated it.
GameObject newObject = GameObject.Instantiate(Resources.Load("prefabs/" + item.gameObject.name) as GameObject, parentProps);
newObject.transform.localPosition = new Vector3(loaded.x, loaded.y, loaded.z);
newObject.transform.localEulerAngles = new Vector3(0, 90f, 0);
newObject.GetComponent<Npc>().UpdateDialog();
On your Robot GameObject use GetComponent to get the NPC script object. Then you are able to modify the values. Make sure that the visibility of your variables is either public or has getters/setters.

unity3d OnMouseDown function

I am new to Unity3D. I am trying to do a simple thing. But not able to do this. I have a .obj file which is a 3d key file. I do the followings:
Import this key (to assets) in unity3D
Add this key to scene (from assets to hierarchy)
Add a script to this key
Add the OnMouseDown() function to this script as follows -
void OnMouseDown()
{
Debug.Log ("clicked...");
}
But when I click the key no message is showing in console. Please tell me what is the problem?
make sure the gameobject is not at layer "Ignore Raycast"
Use the following inside your update function to see raycasting is working fine.
void Update () {
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
Debug.Log ("Name = " + hit.collider.name);
Debug.Log ("Tag = " + hit.collider.tag);
Debug.Log ("Hit Point = " + hit.point);
Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
Debug.Log ("--------------");
}
}
}
The OnMouseDown and it's related functions are all actually older systems. The newer system to use for this is the event system.
Specifically to get mouse clicks you would implement this interface in a class:
http://docs.unity3d.com/460/Documentation/ScriptReference/EventSystems.EventTriggerType.PointerClick.html
But there is actually an easier way to do it without code.
Using this component:
http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
You can then visually forward the mouse click event to something else.
If you don't fully follow what I have said so far, the thing to really start at is learning the UI system. You can find video tutorials here:
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-canvas
And just to make it clear, the UI system is not just for UI. The UI system runs off an EventSystem which is what you want to use to forward input events to any object 3D or 2D in the entire scene. The UI tutorials will explain the usage of that EventSystem.
You need to assign a collider to the 3d object if you want to fire it with OnMouseDown:
OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
This event is sent to all scripts of the Collider or GUIElement.
You can also do like by override click function
public override void OnPointerClick(PointerEventData data)
{
Debug.Log("OnPointerClick called.");
}