Fairly new to Unity, but quickly learning.
I have a spawner which is an empty GameObject which spawns balloons. The balloon object which spawns has a movement script applied which follows waypoints.
The issue I am having is that I can't click and drag the waypoints (also empty game objects) onto the public variables, I can only do this if the balloons have been added into the game Hierarchy first, but I'm spawning the prefab which has the script applied as I don't want it to be in the game yet. If I add the prefab into the game first, I can set the waypoints to the game objects.
How can I fix this so that I am able to add the waypoint game objects to the prefab in the assets?
you can have this method in movement script:
public void SetWayPoints(Transform[] waypoints)
{
this.wayPoints = waypoints;
}
And when you instantiate your prefab just get the component and set the way points;
GameObject baloon = Instantiate(baloonPrefab) as GameObject;
var movementScript = baloon.GetComponent<Movement>();
if(movementScript != null)
{
movementScript.SetWayPoints(waypoints);
}
Note that above code is just an example to illustrate the method of doing it. You script can vary depending on your implementation of waypoints. I assumed that waypoints is an array of transform within a movement script and baloon follows it one by one. Also spawner class have a reference of waypoints in hierarchy and you just pass that array from spawner.
Hope this helps.
Let me know if you have any problem.
Related
I want to spawn several circles on the screen as game objects that float around randomly. To do this I have a prefab that I am instantiating x number of times in a script attached to a main game object. Then I have a 2nd script attached to the prefab to control the random movement. I added a Particle System to the prefab, so that each spawned clone has particles emitting from its edges. I want to know if one object's particles collide with anything, be it another cloned object, a wall, etc. But my OnParticleCollision (in the movement script attached to the prefab) is not logging anything to the console, it seems to not detect particle collisions at all. Maybe I'm not understanding the bigger concept and instantiating multiple instances of the same prefab with a particle system is not the best approach? Or am I making a more obvious minor mistake?
Things I have tried based on other questions:
Send collision messages IS checked
I do not have any colliders marked as triggers
I verified the visual bounds look correct in Scene View
Collision between cloned game objects themselves work fine, it's only the Particle Collisions not working.
My script attached to the prefab:
public class BubbleMove : MonoBehaviour
{
public Rigidbody2D rb;
void Start()
{
rb.velocity = new Vector2(min, max);
ParticleSystem ps = GetComponent<ParticleSystem>();
ps.transform.position = new Vector3(transform.position.x, transform.position.y, 0);
ps.Play();
}
// Update is called once per frame
void Update()
{
}
void OnParticleCollision(GameObject col){
Debug.Log("Collision Particle: " + col);
}
}
Images of my prefab inspector settings for Rigidbody2D, Circle Collider, and Particle System:
Hi I see the rigidbody is not defined in the inspector for the BubbleMove script.
I have a game that has a player ship hovering a fixed distance over a tower defense grid. I'd like the tile beneath the ship to highlight when the ship is over it. By using the OnMouseOver() function, this is quite easy, however, I do not want this to be controlled by the mouse but by the player ship. Is there a way to pass mouse event simulations to a game object? How would i go about assigning this function to the ship?
Like derHugo siad you can use OnTriggerStay(). Altough you can also use a raycast to check if something is underneath the ship:
public void FixedUpdate()
{
RaycastHit hit;
if (Physics.Raycast(transform.position, -transform.up, out hit, 10f))
{
//Here obj will be filled with the gameobject under your ship. You can use this to check for the tag, get components and outline the object.
var obj = hit.transform.gameObject;
}
}
Hope this sets you into the right direction.
I have had this issue for a while and I can not find a solution to fix it. I manage to shoot bullets with trails. To do this, I create a bullet prefab and add a trail renderer to it. I shoot the bullet in another soldier game object by following steps:
Load the bullet prefab:
private void Awake()
{
bulletPrefab = Resources.Load<GameObject>("Enemy/Bullet");
Instantiate bullets from bullet prefab:
private void shoot(){
bullet = Instantiate(bulletPrefab);
bullet.transform.position = this.transform.position;
...
}
Then I update the bullet position according to the calculated angle and bullet speed. The problem is that when I shoot a bullet, the bullet will cast two trails, one trail going toward to the target which I set in the script and the other point to the bullet prefab I load in the Awake function. It seems that the bullet prefab loaded from resource folder does not show in the scene. However it affects the bullets which instantiated from it, making them point a trail to it.
How can I fix this issue?
I have resolved this issue by myself. The cause of this issue is actually pretty simple. I should choose Instantiate(Object original, Vector3 position, Quaternion rotation) rather than Instantiate(Object original).
The first version of instantiate creates the bullet in the given position; relocating the prefab after using the second version will render the unwanted trail.
You can also use the TrailRenderer.emitting option.
just set the emitting to false in by default and right before firing change it to true using myTrail.emitting = true;
I need to create an a furniture dropping app. While I have managed to figure out how to rotate scale the object and detect planes in AR world, I also need to enable the users to be able to choose from a list of furniture, add multiple furniture into the scene and also delete them as when they like. I can't seem to find any tutorials that teach this. There are plenty with ViewAR and Vuforia but since it is a school project, I am restricted to using the ARKit plugin in Unity. Any help in this would be highly appreciated!
So currently what I am doing is, I create an empty gameObject to which I attach a script that has the function call AddObject which is as such:
public void AddObject()
{
GameObject summonedFurni = Instantiate(prefab);
summonedFurni.transform.position = new Vector3 (0f, 0f, 0f);
summonedFurni.transform.localScale = new Vector3 (10f, 10f, 10f);
summonedFurni.AddComponent<Rigidbody> ();
furniList.Add (summonedFurni);
//summonedFurni.SetActive (true);
}
Each type of furniture is a separate gameObject to which the script is attached to.
However, firstly the instantiated objects can't detect the plane and fall right through it. Adding a BoxCollider component simply causes all the furniture to stack up above me in the real world. Also I am unable to delete any objects from the scene as I am unable to keep track of the created objects. Any advice on this?
Adding a BoxCollider component simply causes all the furniture to
stack up above me in the real world.
Try adding z position. By default z is 0. Which is your position as well. That's why it is stacking up above you.
I have designed a complex city with thousand of objects like roads, sideways, buidlings, trees, etc. and imported to Unity3d
I want to put colliders over them, so that when player hit them, it should face collision and should not passes though them
Though it has many objects, if i put one by one collider to each object, it will take so much time. Is there any other way i can just select all of them and put collider.
Also, if i select all object and put colliders (Mesh Collider). it is not adding to the object
Please help
Editor Scripting to the rescue. I would write a tool similar to this example. It uses a Menu Item which can be selected via Window -> Collider Tool. It then runs our custom method to find all meshes, which we want to add a collider to, then adds one and also logs which GameObjects were changed.
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
public static class ColliderTool
{
[MenuItem("Window/Collider Tool")]
public static void RunTool()
{
List<MeshFilter> meshes = findObjectsThatNeedColliders();
for (int i = 0; i < meshes.Count; i++)
{
GameObject go = meshes[i].gameObject;
// Add a collider to each mesh and record the undo step.
MeshCollider collider = Undo.AddComponent<MeshCollider>(go);
// Use the existing mesh as the collider mesh.
collider.sharedMesh = meshes[i].sharedMesh;
Debug.Log("Added a new collider on: " + go.name, go);
}
Debug.Log("Done");
}
private static List<MeshFilter> findObjectsThatNeedColliders()
{
// This list will be filled with all meshes, which require a collider.
List<MeshFilter> meshesWithoutCollider = new List<MeshFilter>();
// Get all meshes in the scene. This only returns active ones.
// Maybe we also need inactive ones, which we can get via GetRootGameObjects and GetComponent.
MeshFilter[] allMeshes = GameObject.FindObjectsOfType<MeshFilter>();
foreach(MeshFilter mesh in allMeshes)
{
if(mesh.GetComponent<Collider>() == null)
meshesWithoutCollider.Add(mesh);
}
return meshesWithoutCollider;
}
}
You might need more complex rules about which objects require a collider and which type it should be, but a simple tool can still save you a lot of time before hand tweaking everything.
You should also consider making this selection based. The Selection class can give you a list of all selected objects. Some ideas:
Add a MeshCollider to all selected objects, if one doesn't already exist.
Add a BoxCollider to selected objects and scale it to an approximate size depending on the transform size or bounding box of the MeshRenderer.
My solution is based on your question about how to add a collider to many objects in a large scene. However, this might not be the overall best solution. Maybe too many MeshColliders hurt physics performance. You will most likely want to approximate most of the colliders with boxes and spheres, but of course you can still write a tool to help you with that.