can i move a gameObject based on just its name? - unity3d

Im using a raycast, putting its RaycastHit into a variable called raycastHit, using that to get the raycastHit.transform.name, could i then use the information from the name of the gameObject to move its position? Or if not, could i get the information from the raycastHit to instead move it straight away? (outside of the function of the raycast) I already looked for an answer, but ive been only finding results on how to move any gameObject.
EDIT1:
my current version of the void:
public RaycastHit raycastHit;
private void HandleHookShotStart()
{
if (TestInputDownHookShot())
{
int gm = LayerMask.NameToLayer("Ground");
int lwm = LayerMask.NameToLayer("LightWeight");
int layers = 1 << gm | 1 << lwm;
if (Physics.Raycast(Shoulder.transform.position, cam.transform.forward, out raycastHit, float.PositiveInfinity, layers))
{
//Hit Something
debugHitpointTransform.position = raycastHit.point;
hookshotPosition = raycastHit.point;
hookShotSize = 0f;
HookShotTransform.gameObject.SetActive(true);
HookShotTransform.localScale = Vector3.zero;
if (raycastHit.collider.gameObject.layer == gm)
{
Debug.Log("ground");
hitType = HitType.Ground;
}
else if (raycastHit.collider.gameObject.layer == lwm)
{
//Debug.Log("lightweight");
hitType = HitType.Lightweight;
goToMove = raycastHit.collider.gameObject;
string raycastHitObjectname = goToMove.name;
}
state = State.HookShotThrown;
}
}
}
EDIT2:
private void HandleHookShotGrab()
{
GameObject raycastHitObject = GameObject.Find(raycastHitObjectName);
}
error is on raycastHitObjectName
error:
CS0103 the name 'raycastHitObjectName' does not exist in the current context

You dont need to find your gameobject by name, you can add a ref to the gameobject in the script from which you would be moving the gameobject from.
public RaycastHit raycastHit;
public GameObject goToMove;//drag in to your gameObject component in unity editor
private void HandleRayCast()
{
if (Physics.Raycast(transform.position, cam.transform.forward, out raycastHit))
{
//Hit Something
goToMove.transform.position = raycastHit.point;
}
}
If you still need to find the gameObject by name, you can still get in the Start phase.
void Start() {
goToMove = GameObject.Find("yourGameObjectName");
}
Don use GameObject.Find in Updates. Use it to keep a reference to your gameObjects at the initialization phase of your Monobehaviours. In small games or playaround projects its not a big deal, but it is an "expensive" command, so needs to be used with care.

Related

Got an error about RayCast when getting the mouse position

NullReferenceException: Object reference not set to an instance of an object
DragBlock.Update () (at Assets/Script/DragBlock.cs:13)
using UnityEngine;
public class DragBlock : MonoBehaviour
{
private bool isBeingHeld = false;
private Vector3 startPos;
private Transform heldObject;
private void Update()
{
if (Input.GetMouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.CompareTag("Blocks"))
{
isBeingHeld = true;
startPos = hit.transform.position;
heldObject = hit.transform;
}
}
}
if (isBeingHeld)
{
Vector3 currentMousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
currentMousePos.z = heldObject.position.z;
heldObject.position = currentMousePos;
}
if (Input.GetMouseButtonUp(0))
{
isBeingHeld = false;
}
}
}
I tried to change the group of the object tried to check the position and so on in this code it should check the position of the mouse and drag the object with the Blocks tag
If line 13 is this one...
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
... then the only part of the code that could realistically be null is the Camera.main property. Camera.main is returned by Unity when Unity looks for, and finds, a Camera in your scene that's tagged "MainCamera".
I suggest that you might have removed or changed that tag of the Camera, or a parent of the camera, and now Unity can't find an appropriate camera.
This is echoed in the online documentation https://docs.unity3d.com/ScriptReference/Camera-main.html

Raycast passes through objects

My raycast passes through objects with mesh collider/box collider with layers.
I can't seem to find a solution. Please help me.
I tried different solutions but the raycast still passes through objects. Even objects behind intractable objects are also being called out.
public float maxRayDistance = 2.0f;
public LayerMask layerMask;
public GUISkin mySkin;
public bool showGui = false;
public RaycastHit hit;
void Update()
{
Vector3 dir = gameObject.transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, dir, out hit, maxRayDistance, layerMask))
{
showGui = true;
if (Input.GetButtonDown("Use"))
{
Invoke("SendAction", 0.3f);
}
}
else
{
showGui = false;
}
}
public void SendAction()
{
GameObject target = hit.collider.gameObject;
target.BroadcastMessage("Action");
}
Layers
Object with Layer other
You need to check IsTrigger in Collider (UnityEditor) to have Raycast working on your object.
If this is not working, there is a setting in your projets about Raycast that you might have turned off in Physics Settings.

How to create an object to follow my Player

I'm looking for a way to do something like this:
Gradius
In this game, orbs are following the player. How to do this in Unity where my orbs are following my Player.
Thanks!
A simple solution that i have thought is, lets say i have a player and it is holding it's previous position at start and whenever it moves lets say 5 units then we say to the object that will follow it to follow it's previous position and then we update player's previous position to it's current position and we follow same steps.
I have created a simple test scene and followed these steps :
I have created 2 game objects called Player and FollowPlayer
I have wrote the script below and attached it to the Player
public static event Action<Vector3> FollowMe;
[SerializeField] private float _followDistance;
private Vector3 _previousPosition;
private void Start()
{
_previousPosition = transform.position;
}
private void Update()
{
if(Vector3.Distance(transform.position,_previousPosition) > _followDistance)
{
if(FollowMe != null)
{
FollowMe.Invoke(_previousPosition);
}
_previousPosition = transform.position;
}
}
Then for the FollowPlayer object i have wrote the script below and attached to it
private void Start()
{
Player.FollowMe += OnFollowMe;
}
private void OnDestroy()
{
Player.FollowMe -= OnFollowMe;
}
private void OnFollowMe(Vector3 position)
{
transform.position = position;
}
Again, this is a simple follow script same logic in the Gladius game and i am sure you can use this idea and make it more generic and usable.

Unity network transform problem - Client spawning objects at wrong location

I'm working on a Fortnite-esque game for Unity.
Player spawns in, has ability to spawn cubes to make a "base".
Everything works perfectly well in mono but I'm having a strange issue with networking. On the server, my player can spawn the cubes perfectly according to the Raycast hitpoint while on the client, even though the player is a clone of the Player prefab, the spawned objects always either end up at the world origin, rather than Raycast hitpoint -or- if I remove if (!isPlayLocal) {return;} from the player's script containing Raycast info, the cube spawns inaccurately and without its corresponding material.
I'll try and pinpoint the code so I can place it here but I imagine it could be a number of things.
Local Player Auth is checked off on spawn prefabs and all prefabs have been registered in Network Manager.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class BuildingSystemNew : NetworkBehaviour
{
[SerializeField] private Camera playerCamera;
[SerializeField] private GameObject blockTemplatePrefab;
[SerializeField] private GameObject blockPrefab;
[SerializeField] private Material templateMaterial;
[SerializeField] private LayerMask buildableSurfacesLayer;
private bool buildModeOn = false;
private bool canBuild = false;
private bool crossHairOn = false;
private BlockSystem bSys;
public Texture2D crosshairImage;
private int blockSelectCounter = 0;
private GameObject weapon;
private Vector3 buildPos;
private GameObject currentTemplateBlock;
private void Start()
{
bSys = GetComponent<BlockSystem>();
}
private void Update()
{
if (isLocalPlayer == false)
return;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
if (Input.GetKeyDown("e"))
{
buildModeOn = !buildModeOn;
if (buildModeOn)
{
// weapon.SetActive(false);
crossHairOn = true;
}
else
{
// weapon.SetActive(true);
crossHairOn = false;
}
}
if (Input.GetKeyDown("r"))
{
blockSelectCounter++;
if (blockSelectCounter >= bSys.allBlocks.Count) blockSelectCounter = 0;
}
if (buildModeOn)
{
RaycastHit buildPosHit;
if (Physics.Raycast(playerCamera.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)), out buildPosHit, 10, buildableSurfacesLayer))
{
Vector3 point = buildPosHit.point;
buildPos = new Vector3(Mathf.Round(point.x), Mathf.Round(point.y), Mathf.Round(point.z));
canBuild = true;
}
else
{
Destroy(currentTemplateBlock.gameObject);
canBuild = false;
}
}
if (!buildModeOn && currentTemplateBlock != null)
{
Destroy(currentTemplateBlock.gameObject);
canBuild = false;
}
if (canBuild && currentTemplateBlock == null)
{
currentTemplateBlock = Instantiate(blockTemplatePrefab, buildPos, Quaternion.identity);
currentTemplateBlock.GetComponent<MeshRenderer>().material = templateMaterial;
}
if (canBuild && currentTemplateBlock != null)
{
currentTemplateBlock.transform.position = buildPos;
if (Input.GetMouseButtonDown(0))
{
CmdPlaceBlock();
}
else if (Input.GetMouseButtonDown(1))
{
CmdDestroyBlock();
}
}
}
[Command]
public void CmdPlaceBlock()
{
GameObject newBlock = Instantiate(blockPrefab, buildPos, Quaternion.identity);
Block tempBlock = bSys.allBlocks[blockSelectCounter];
newBlock.name = tempBlock.blockName + "-Block";
newBlock.GetComponent<MeshRenderer>().material = tempBlock.blockMaterial;
NetworkServer.SpawnWithClientAuthority(newBlock, connectionToClient);
}
[Command]
private void CmdDestroyBlock()
{
RaycastHit hit;
Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
var objectHit = hit.collider.gameObject;
if (hit.collider.gameObject.tag == "Block")
{
Destroy(objectHit);
}
}
}
void OnGUI()
{
if (crossHairOn == true)
{
float xMin = (Screen.width / 2) - (crosshairImage.width / 2);
float yMin = (Screen.height / 2) - (crosshairImage.height / 2);
GUI.DrawTexture(new Rect(xMin, yMin, crosshairImage.width, crosshairImage.height), crosshairImage);
}
}
}
The Problem
You are calling
[Command]
public void CmdPlaceBlock()
{
GameObject newBlock = Instantiate(blockPrefab, buildPos, Quaternion.identity);
Block tempBlock = bSys.allBlocks[blockSelectCounter];
newBlock.name = tempBlock.blockName + "-Block";
newBlock.GetComponent<MeshRenderer>().material = tempBlock.blockMaterial;
NetworkServer.SpawnWithClientAuthority(newBlock, connectionToClient);
}
without a parameter.
A Command is called on the client but executed on the Server
=> with the local variables of the Server!
So e.g. buildPos will always have the default value 0,0,0 on the server since due to
if(!isLocalPlayer) return;
later the line
buildPos = new Vector3(Mathf.Round(point.x), Mathf.Round(point.y), Mathf.Round(point.z));
is never executed on the server. The same also applies e.g. to blockSelectCounter and probably other values your CmdPlaceBlock depends on.
Solution
You should pass your client's buildPos value (and also all other values that are different on client and server) to the server command so the server knows at which correct position the new object should be placed:
//...
if (Input.GetMouseButtonDown(0))
{
CmdPlaceBlock(buildPos);
}
//...
[Command]
public void CmdPlaceBlock(Vector3 spawnPosition)
{
GameObject newBlock = Instantiate(blockPrefab, spawnPosition, Quaternion.identity);
Block tempBlock = bSys.allBlocks[blockSelectCounter];
newBlock.name = tempBlock.blockName + "-Block";
newBlock.GetComponent<MeshRenderer>().material = tempBlock.blockMaterial;
NetworkServer.SpawnWithClientAuthority(newBlock, connectionToClient);
}
I added only an example for the position knowing it will always be different on client and server. But it also might apply to your other values like e.g. blockSelectCounter.
Do this change for all values that have to be the ones of the client and not the ones of the server.
Note that the types that can be passed between networking methods are limited! You can't pass e.g. any component references.
The allowed argument types are;
Basic type (byte, int, float, string, UInt64, etc)
Built-in Unity math type (Vector3, Quaternion, etc),
Arrays of basic types
Structs containing allowable types
NetworkIdentity
NetworkInstanceId
NetworkHash128
GameObject with a NetworkIdentity component attached.
Additional Hints
For readability and amount of lines you should change things like e.g.
if (buildModeOn)
{
// weapon.SetActive(false);
crossHairOn = true;
}
else
{
// weapon.SetActive(true);
crossHairOn = false;
}
to simply
// weapon.SetActive(!buildModeOn);
crossHairOn = buildModeOn;
and check bools not like
if (isLocalPlayer == false)
but rather
if(!isLocalPlayer)
It simply reads/writes easier ;)

Create a copy of an gameobject

How do you create a copy of an object upon mouse click in Unity3D?
Also, how could I select the object to be cloned during run-time? (mouse selection preferable).
function Update () {
var hit : RaycastHit = new RaycastHit();
var cameraRay : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (cameraRay.origin,cameraRay.direction,hit, 1000)) {
var cursorOn = true;
}
var mouseReleased : boolean = false;
//BOMB DROPPING
if (Input.GetMouseButtonDown(0)) {
drop = Instantiate(bomb, transform.position, Quaternion.identity);
drop.transform.position = hit.point;
Resize();
}
}
function Resize() {
if (!Input.GetMouseButtonUp(0)) {
drop.transform.localScale += Vector3(Time.deltaTime, Time.deltaTime,
Time.deltaTime);
timeD +=Time.deltaTime;
}
}
And you'll want this to happen over course of many calls to Update:
function Update () {
if(Input.GetMouseButton(0)) {
// This means the left mouse button is currently down,
// so we'll augment the scale
drop.transform.localScale += Vector3(Time.deltaTime, Time.deltaTime,
Time.deltaTime);
}
}
The simplest way (in c#) would be something like this:
[RequireComponent(typeof(Collider))]
public class Cloneable : MonoBehaviour {
public Vector3 spawnPoint = Vector3.zero;
/* create a copy of this object at the specified spawn point with no rotation */
public void OnMouseDown () {
Object.Instantiate(gameObject, spawnPoint, Quaternion.identity);
}
}
(The first line just makes sure there is a collider attached to the object, it's required to detect the mouse click)
That script should work as is, but I haven't tested it yet, I'll fix it if it doesn't.
If your script is attached to a GameObject (say, a sphere), then you can do this:
public class ObjectMaker : MonoBehaviour
{
public GameObject thing2bInstantiated; // This you assign in the inspector
void OnMouseDown( )
{
Instantiate(thing2bInstantiated, transform.position, transform.rotation);
}
}
You give Instantiate( ) three parameters: what object, what position, how is it rotated.
What this script does is it instantiates something at the exact position & rotation of the GameObject this script is attached to. Oftentimes you will need to remove the collider from the GameObject, and the rigidbody if there is one. There a variations in ways you can go about instantiating things, so if this one doesn't work for you I can provide a different example. : )