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.
Related
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.
I have an object called 'Player' in my scene.
I also have multiple objects called 'Trees'.
Now I'd like whenever the user clicks on a 'Tree', the 'Player' to move slowly to that position (using Lerp or moveTowards) is both O.K for me.
Now I have 2 issues with this code:
I'd like this code to be generic
Whenever I click a tree object I'd like this to move the player towards that tree. I don't want to write up this script and attach it to each tree object.
Where should I put the script?
Currently I attach this code to every tree object.
How should I write it down so that it applies to every tree object
in the scene?
If another click is made while moving, cancel previous movement and start moving to new position
How should I write it so that if I click on another object while the
player is moving towards another object that was clicked, the players stops moving towards it's previous position, and starts
moving towards the new point.
I'm having some trouble adjusting to the new UnityScript thingy. I come strictly from a Javascript background and it really seems like the 2 of them are languages with very different semantics. So if someone answers this with code(which is what I would want:) ), I'd appreciate some verbose comments as well.
I currently do this:
var playerIsMoving = false;
Public playerObject: Gameobject; //I drag in the editor the player in this public var
function update(){
var thisTreePosition = transform.point; //this store the X pos of the tree
var playerPosition = player.transform.point;
if(playerIsMoving){
player.transform.position = Vector2.MoveTowards(playerPosition, thisTreePosition, step);
}
}
function OnMouseDown(){
playerIsMoving = true;
}
I'm writing this from home where I don't have Unity and I forgot the code syntax therefore expect the above code to have typos or issues, at work it works just fine, apart from being very crude and unsophisticated.
I would recommend you to put the movement Script on the player. And how abour using Raycast to the test whether you hit a tree?
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html
Vector3 positionToWalkTo = new Vector3();
void Update() {
if (Input.GetMouseButtonDown(0)) {
RaycastHit hit;
Ray ray = new Ray(transform.position, direction);
if (Physics.Raycast(ray, out hit))
if (hit.gameObject.tag.Equals("tree")){
positionToWalkTo = hit.gameObject.transform.position;
}
}
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, positionToWalkTo, step);
}
To get something like this running, you should tag all the trees.
'JavaScript'
var target: Transform;
// Speed in units per sec.
var speed: float;
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
// Raycasting is like shooting something into the given direction (ray) and hit is the object which got hit
if (Physics.Raycast(ray, hit)) {
if (hit.gameObject.tag = "tree")
target = hit.gameObject.transform.position; // Sets the new target position
}
}
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
Okay, so I've done it myself (with some help from Freshchris's answer).
Here it is:
var target:Vector2;
var targetObject:GameObject;
var initialY:float;
var tolerance = 1;
var speed = 3;
var isMoving = false;
function Start(){
target = transform.position;
}
function Update () {
if (Input.GetMouseButtonDown(0)) {
var hit: RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); //raycast the scene
// if we hit something
if (hit.collider != null) {
isMoving=true; //it should start moving
targetObject = hit.collider.gameObject; //the target object
target = hit.collider.gameObject.transform.position;
}
}
// The step size is equal to speed times frame time.
var step = speed * Time.deltaTime;
// if it should be moving - move it - and face the player to the correct direction
if (isMoving){
if(transform.position.x>target.x){
gameObject.transform.localScale.x = -0.7;
}else {
gameObject.transform.localScale.x = 0.7;
}
transform.position = Vector2.MoveTowards(transform.position, target, step);
}
// if it reached the target object by a specified tolerance, tell it to stop moving
if(isMoving){
if((transform.position.x < target.x+tolerance)&&(transform.position.x > target.x-tolerance)){
print("position reached"); //this is fired just once since isMoving is switched to false just one line below
print(targetObject);
isMoving=false;
}
}
}
I'm following a tutorial into making a game in Unity, and I've encountered a peculiar problem with my JavaScript/UnityScript:
#pragma strict
var Health = 100;
function ApplyDamage (TheDamage : int)
{
Health -= TheDamage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
Destroy (gameObject);
}
That is my script for the enemy game object, however, despite me having logic for a melee system where you have to click him twice and he dies, it doesn't work, and whenever my player character walks into the enemy it is destroyed automatically without clicks.
I'll post my melee system here too:
#pragma strict
var TheDamage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
function Update ()
{
if (Input.GetButtonDown("Fire1"))
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
(
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions. DontRequireReceiver)
);
}
}
Can anyone help me with this? It seems like a random error.
The code you posted for your melee system is missing braces {} for the first if statement, causing it to only execute var hit : RaycastHit; when the mouse is clicked, and execute the rest of the body every time Update is called.
Your code has a problem. You are not actually using the if statement correctly. This is because you didn't use {}. I will post how your code is being interpreted below:
if (Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
}
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions. DontRequireReceiver);
}
}
Obviously that is not what you intended. Remember if you don't put the brackets then only the line directly after the if will execute when the if condition is met, the rest will execute independently of that if.
Solution
if (Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
// Intentionally left the brackets out of this if to show you.
if(Distance < MaxDistance)
hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions. DontRequireReceiver);
}
}
So I intentionally left out the brackets on that inside if to show you how to use it without brackets. If you are unsure, just add brackets, it will make your code cleaner and easier to understand and you will avoid bugs.
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?