Flip a 2d GameObject and place it on exact the same position works well on both sides.
Rotating 2d GameObject works well on both sides
However, if i flip the GameObject, by executing Cmd_DoTheSpawn, the rotation is not reflected on the "other" client.
I would need help to get this work.
Here is the code I use:
[Command]
void Cmd_DoTheSpawn(GameObject myGameObject) {
// Check if front or back?
char lastChar = myGameObject.tag[myGameObject.tag.Length - 1];
if (lastChar == 'B') {
convertedObjectTag = unet_Back2Front [myGameObject.tag];
} else {
convertedObjectTag = unet_Front2Back [myGameObject.tag];
}
GameObject my1 = Resources.Load (convertedObjectTag) as GameObject;
float z = myGameObject.transform.localEulerAngles.z;
//var go = (GameObject)Instantiate (my1, myGameObject.transform.position, myGameObject.transform.localRotation);
var go = (GameObject)Instantiate(my1, myGameObject.transform.position, Quaternion.Euler(0f, 0f, z));
go.name = go.name.Remove(go.name.Length - 7); // Remove the '(Clone)' in name
NetworkServer.SpawnWithClientAuthority(go, base.connectionToClient);
print ("myGameObject: " + myGameObject.transform.position);
if (myGameObject == null)
print ("myGameObject NULL" + myGameObject.transform.position);
Rpc_DoTheRot (go, myGameObject);
myNetID = myGameObject.GetComponent<NetworkIdentity> ();
Cmd_DestroyGameObject (myNetID.netId);
}
Thanks to #LumbusterTick i did get it to work, meaning the rotation is ok. However, i did get another problem that i do not fully understand.
I added the following code:
[ClientRpc]
public void Rpc_DoTheRot(GameObject newGO, GameObject oldGO) {
print ("Rpc_DoTheRot");
if (newGO == null)
print ("newGO NULL");
if (oldGO == null)
print ("oldGO NULL");
newGO.transform.rotation = oldGO.transform.rotation;
}
...which i call after spawn but prior to destroy, see updated code above.
It does place the flipped prefab in correct rotation but i do get the following messages:
oldGO NULL
as well as:
NullReferenceException: Object reference not set to an instance of an object
I know perfectly well what that means but not why it happen as all values of the myGameObject is nulled when i send them. ...and i do it prior to destroy.
I do not understand why it is null but still effect the rotation on the new object.
From you command function after you network spawn call a clientrpc function and pass instantiated gameobject to it and change its rotation there.
Related
I've been trying to get a script working to check if my player is below a certain Y lvl, for a platformer. so it can be respawned to the beginning, But how do I put the y lvl inside a variable to check it? i cant figure it out lol
In the Update() run something like:
if(player.transform.position.y < 1)
{
//do something
}
where 'player' is the GameObject in question.
I am assuming you want to just want to compare (==, <=, >=, all that jazz is what I mean by comparing just in case you were not aware) the Y value to something like 10 for example. This is easy and you don't even need a variable necessarily for this.
//For the object position relative to the world
if(transform.position.y == 10) //"transform" gives you acces to the transform component
{ //of the object the script is attached to
Debug.Log("MILK GANG");
}
//For the object position relative to its Parent Object
if(transform.localPosition.y == 10)
{
Debug.Log("MILK GANG");
}
If you want to change the value of the position of your object then
transform.position = new Vector2(6, 9)//Nice
//BTW new Vector2 can be used if you dont
//want to assign a completely new variable
However, if you want to get a reference (Basically a variable that tells the code your talking about this component) to it.
private Transform Trans;
void Awake() //Awake is called/being executed before the first frame so its
{ //better than void Start in this case
Trans = GetComponent<Transform>();
Trans.position = new Vector2(69, 420); //Nice
}
This is the code way of doing it but there's another way that uses Unity
[SerializeField] private Transform Trans;
//[SerializeField] makes the variable changeable in Unity even if it is private so you
//can just drag and drop on to this and you good to go
Hope this help
if it doesn't
then welp I tried lel
You can use a script added to gameobject to check transform.position of the object.
if(transform.position.y < ylvl)
{
//do something
}
where ylvl is the integer of the height you want to check
I have a enemy AI script and a small part of the code isn't working. It's supposed to search for a game object with the Player tag when the target no longer exists. So, when the player collects a power up and a new player prefab spawns in its place it will search. However, it is not finding one and I don't know why.
Here is the part of the code that doesn't work:
IEnumerator SearchForPlayer (){
GameObject sResult = GameObject.FindGameObjectWithTag ("Player");
if (sResult = null) {
yield return new WaitForSeconds (0.5f);
StartCoroutine (SearchForPlayer ());
}
else {
target = sResult.transform;
searchingForPlayer = false;
StartCoroutine (UpdatePath());
return false;
}
}
I know that my prefab has got the correct tag and that it is definitely getting to that part of the script. Does any one have any ideas why it's not working?
if (sResult = null) {
Should be
if (sResult == null) {
Single = is an assignment operator so you are setting the result to null instead of checking if it is null.
I'm making a game where the player touches the screen and an object instantiates . However, I only want the object to instantiate if the Raycast hits an object that is on a specific layer (or of a specific tag if that is easier). I can get the ray to cast out and instantiate a prefab where I'd like it, but when I add in the section of checking for the layer, it sends me an error (NullReferenceException:Object reference not set to an instance of an object). This seems like such a simple thing but I can't get it to work. Any help would be GREATLY appreciated!
var box : Transform;
function Update ()
{
if (Input.GetMouseButtonDown(0))
{
if(roomController.noMore == false){
var hit : RaycastHit;
var mousePos : Vector3 = Input.mousePosition;
mousePos.z = 9;
var worldPos : Vector3 = camera.ScreenToWorldPoint(mousePos);
Debug.Log("Mouse pos: " + mousePos + " World Pos: " + worldPos + " Near Clip Plane: " + camera.nearClipPlane);
if(hit.collider.gameObject.layer == "Ground" && HierarchyType.collider != null){
clone = Instantiate(box, worldPos, Quaternion.identity);
noMore = true;
Destroy(this);
}
}
}
}
Try use Physics.Raycast.
var hit : RaycastHit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit))
{
if (hit.collider != null && hit.collider.gameObject.layer == LayerMask.NameToLayer("Water"))
{
Debug.Log("Instantiate");
}
}
but when I add in the section of checking for the layer, it sends me
an error (NullReferenceException:Object reference not set to an
instance of an object).
This is probably because actually you are not casting any ray and checking that an hit actually occurred( avoid the relative collider field could be null).
I suggest you to have a look at Physics.RayCast overloads, it could be significant more efficient to specify the layer (through LayerMask) while casting instead of filtering the results later.
I have a number of sprites on top of each-other on the game board. When i use the mouse and select the sprite on top, all sprites under the mouse position is selected. My question is how to only select the sprite that I click on and not catch the ones below?
Here is the code i am using for my tests, attached to the sprites:
function Update () {
if(Input.GetMouseButtonDown(0)) {
var theActualMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// print("ALL MousePosition: " + theActualMousePosition);
if(collider2D.OverlapPoint(theActualMousePosition)) {
// print("HIT theActualMousePosition: " + theActualMousePosition);
print(collider2D.gameObject.tag);
}
}
}
The results you are getting are completely expected as your code is placed on the GameObjects, what you should do is to push your script out of those objects or use another function than OverlapPoint (because overlap point does not check for collision it simply checks if you are in the bounds of an object, which means it is valid for all object)
Some ideas :
Using OnMouseDown should provide you with an event only for the first collider encountered
Using A raycast from the camera : Camera.ScreenPointToRay should be able to be used for a raycast and then to check only the first collider encountered
Using Layers depending on layer collision Order.
EDIT :
Or you could also cast the ray the other way around :
function Update () {
if(Input.GetMouseButtonDown(0)) {
var theActualMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 direction = (transform.Position-theActualMousePosition).normalized;
Ray ray = Ray(transform.Position,direction)
if(Physics.Raycast(ray) == null) {
//Then there is nothing between the object and the camera then the object is the first one
print(collider2D.gameObject.tag);
}
}
}
I would like to associate the same script to different empty objects I just use as placeholders in the game. The aim is to exploit their positions so that when the user touch a point in the screen, close to one of these objects, a dedicate GUI appears. The problem is that though the two objects are different their scripts seem to influence each other so that when the game is running and I touch one of these two objects both the gui appears. What am I doing wrong?
....
private var check: boolean;
var topCamera : Camera;
var customSkin : GUISkin;
function Update () {
if (Input.GetMouseButtonDown(0)){
if(Input.mousePosition.x > this.transform.position.x - Screen.width*0.20 && Input.mousePosition.x < this.transform.position.x + Screen.width*20){
if(Input.mousePosition.y > this.transform.position.y - Screen.height*0.2 && Input.mousePosition.y < this.transform.position.y + Screen.height*0.2){
check = true;
}
}
}
if(check){
//the camera zooms forward
}else{
//the camera zooms backward
}
}
function OnGUI () {
if (this.check){
var w = Screen.width;
var h = Screen.height;
var bw = 0.083;
var bws = 0.001 *w;
GUI.skin = customSkin;
GUI.Box(new Rect(w*0.6,h*0.3,w*0.38,h*0.45), "Stuff");
customSkin.box.fontSize = 0.04*h;
customSkin.textField.fontSize = 0.08*h;
customSkin.button.fontSize = 0.04*h;
textFieldString = GUI.TextField (Rect (w*0.62, h*0.39, w*0.34, h*0.1), textFieldString);
if (GUI.Button (Rect (w*0.62,h*0.50, w*bw, h*0.1), "+")) {
if (this.check){
this.check=false;
}else{
this.check = true;
}
//...
}
//...
}
This is probably not working, because you are comparing apples with oranges in your Update() function. Input.mousePosition returns the the position in 2D pixel coordinates and transform.position returns the GameObject's position in 3D world coordinates.
To check if you clicked on an object, you need to attach a Collider to the game object in question and test for collisions using a Raycast in your script. Here is the relavant example from the documentation in JavaScript:
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, 100)) {
print ("Hit something");
}
The cool thing about this approach is that we are checking for collisions between the Collider and the ray. If you only want to see if you clicked near the GameObject, just make the Collider larger than the GameObject. No need for messing around with inequalities!
If your objective is to click somewhere close to the object and not only at the object, then you have some configurations (positions of those objects in space) where there are space that are close enough to both objects for their GUI to appear and therefore you need some script to decide which one is closer.
I suggest you to implement a monobehaviour that is a singleton that would track those clicks and measure the distance of all objects, to get the closest.
Reading again your post, I think you want to get the GUI just when you click at the object, and when you do this you get both GUIs. I think that's happening because wrong calculation of the area that makes check to go true.
Can you give more details? Is there some space where there shouldn't have GUI messages when clicked, or is everything filled by those objects?