Particle at Input.mousePosition Point - unity3d

I am trying to understand this link which, I believe, is supposed to create a particle where you left-click your mouse. Am I correct?
I have attached the script to an empty game object, so it runs when I run my Unity project. I added a new Particle System game object and dragged it onto the public field of the inspector. Is this the right way of doing it, or should I assign something else to the public variable?
The particle system starts firing up as soon as I run the project, so how do I stop that and then make it begin when and where I have clicked my mouse curser?
public GameObject particle;
if ( Input.GetButtonDown("Fire1") )
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if ( Physics.Raycast(ray) )
Instantiate(particle, transform.position, transform.rotation);
Debug.Log(Input.mousePosition.x);
}

You should either turn off looping in the particle system or Stop() or Pause() it on Awake() in your script and Play() when clicking the fire button.
Check this page: http://docs.unity3d.com/ScriptReference/ParticleSystem.html
The sample that you have provided doesn't take a particle system, but some 'projectile' prefab, it basically spawns the prefab instance when you click mouse button. It is just an example of very very basic shooting mechanic. I believe that naming on that script could have been better. (I believe that this sample was taken from one of Unity's Merry Fragmas videos, they used some kind of particle system in a similar manner, you'll have to configure the particle system to burst a bunch of particles and not loop after that if you want to use it this way)

Related

playing particle system in Unity

I am using Unity3D to develop for the HTV Vive using SteamVR. I have downloaded an asset from the asset store with explosion effect created using a particle system. I want to play the particle animation when an object is destroyed. Here is the code I am, unsuccessfully, using.
private void OnDestroy() {
explosion.GetComponent<ParticleSystem>().Play();
}
Explosion is a public variable of type GameObject set from the inspector. I drop the particle system object there.
Why is it not working? anyone a good recommendation on a short tutorial to learn to use (not to create) particle effects?
Thanks
view of the hierarchy
I have tried this with the PS as a child of the target and as an independent object.
view of the inspector (Target)
view of the inspector (particle system)
edit: for some reason, the particle effect is destroyed right after the scene starts.
Try making the explosion effect into a prefab and instantiate it when destroyed.
GameObject explosion; // Prefab asset
private void OnDestroy() {
Instantiate(explosion, transform.position, Quaternion.identity);
}
Also, don't forget the stop action to Destroy.

Using triggers attached to enemy to make enemy jump in a 2.5d platformer

so I've got an enemy in my 2.5d platformer, that I already got to follow the player around, now I want him to jump over small obstacles, and honestly, I have no idea how to. My enemy is a sphere with rigidbody and a box collider scaled 2x in X axis, effectively making it 2 times wider than the sphere. I want this box collider to be a trigger and for it to launch a AddForce.
Where do I put the code for the trigger - if I put it in void update() wouldn't it apply force until the sphere is above the obstacle? I only want it to apply the force once.
Also, what would the code for the trigger look like?
So far I only got figured out how to find the rigidbody by getcomponent and apply force to it, and I watched a video on triggers, but it didn't really help :/
Use OnTriggerEnter method like this:
void OnTriggerEnter(Collider other)
{
if (other.tag == "IMakeEnemyJumpBeforeAnObstacle")
{
// add force here
}
}
Note: this code should be in your enemy script. Plus use IMakeEnemyJumpBeforeAnObstacle tag on the trigger object which will be placed right before the obstacle. And make sure both the enemy and the other object have property IsTrigger checked from inspector.
Learn More
Hope this helps

Camera rotation within a prefab

I am trying to create a multiplayer game in unity3d using a diablo like camera system. So wherever I click on the screen. The system it was working fine in singleplayer where I did not need to include a camera in a player prefab. However Now I am facing the problem where my camera rotation is also affected by the rotation of my prefab parent. The hierarchy looks like this:
There is a script added to the camera that looks like this:
using UnityEngine;
using System.Collections;
public class MainCameraComponent : MonoBehaviour {
public GameObject Reaper;
private Vector3 offset;
void Start () {
offset = transform.position - Reaper.transform.position;
}
// Update is called once per frame
void LateUpdate () {
transform.position = Reaper.transform.position + offset;
}
}
When I run the game the camera always stays behind my character, while I want it to always stay at the same rotation. so if I order my character to walk north I would see his back, if it would walk south I wanted to see the front.
notice how the shadow changes, (thus the rotation) but i always face the back of my model. TLDR: I want my child camera to ignore the rotational change of its parent and be static. whilst letting my camera position be guided by its parent. as far as I know it seems impossible to make a networkmanager instantiate a new player prefab and attach a camera afterwards on the same hierarchy level.
Any help would be greatly appreciated.
However Now I am facing the problem where my camera rotation is also
affected by the rotation of my prefab parent
That's because your Camera is a child of the player so it will do whatever the Player/parent is doing.
Your camera should not be under the player. Remove it and make sure it is not a child of the Player or any other Object. That script should work or make the camera follow the player without making the Camera the child of the GameObject.
Remove Player = GameObject.Find ("Player"); from the LateUpdate function. You have already done this in the Start function, so it is unnecessary to do it in the LateUpdate function. GameObject.Find ("Player"); should never be called directly from the LateUpdate function. It will slow down your game.
EDIT:
After reading your comment, it looks like you want to instantiate a player with the camera. You can do this without making the camera a child of your player.
Your current Setup:
Player
Model
Projectile
Projectile Mesh
Camera
So your camera is under Player.
You new Setup:
PlayerPrefab
Player
Model
Projectile
Projectile Mesh
Camera (Attach script in question to this)
Camera is now PlayerPrefab instead of Player. Now, you can instantiate PlayerPrefab and move the Player with a script. I don't know what your move script looks like but it should be attached to the Player not PlayerPrefab. The PlayerPrefab is used to hold everything so that it can be easily instantiated as one. The code in your question should be attached to your Camera.

Unity particle system only plays if i hit SIMULATE button

I have a prefab with a particle system attached. In the code I play the particle by using this code
ps.enableEmission = true;
When i run the game, and that code executes, the particle emitter does not emit anything in the "game" window unless i press simulate button in the "scene" window.
Anybody knows why?
For the emission property to work the particle system has to actually be playing. To do this you can either enable Play On Awake in the ParticleSystem component or you use the Play method on an instance of the ParticleSystem component.
As a side note, if you are using 5.3+ the enableEmission property is now obsolete and you may want to consider using the emission property. One thing to keep in mind when using this property is you have to assign it to a variable before attempting to modify it:
public ParticleSystem _ps;
...
private void Update()
{
ParticleSystem.EmissionModule module = _ps.emission;
module.enabled = true;
}
UPDATE #1
In response to your tractor beam example in the comments I would probably suggest using SetActive on the game object that has the ParticleSystem component. By using SetActive it will prevent extra particles being emitted and will destroy any that are active, i.e. those currently in the scene.
If you use the Emission property, then it will prevent the emission of extra particles, but it will not destroy any that have already been emitted.
One other approach would be to use Play and Stop methods but these, as with the emission property, will not destroy any active particles. If you use these methods, then some things to watch out for are:
If the Prewarm option is not enabled, then Play does not start
emitting particles (not sure why this happens)
The isPlaying property will remain true as along as there are
active particles in the scene. Once these die, then it will
be set to false
The Stop method will not destroy particles active in the scene
If the ParticleSystem has stopped and you call Play while there are particles active
in the scene, then all active particles are destroyed and the
ParticleSystem starts emitting a new set of particles
Check out to see if particle GameObject is active when you call ps.enableEmission = true; on it. To show or hide particles i normally use gameObject.setActive() not ps.enableEmission.
My particle was not simulating and after I changed the culling Mode to Automatic everything worked. The default is pause and catch-up.

Player dies when hitting the floor

I am trying to make a game in unity, and I am new to unity and coding, and I have started making a game, I have made some progress on it but I am having trouble finding some of my answers on youtube and the unity forum, and sometimes when I do, I still can't get things to work. So this is what I'm trying to do.
I have a map and the player is on top of the tower, I want the player to fall and when hitting the ground, dies with it displaying game over, What could I do to make this happen and what script?
So i have this now,
// Ground.cs: Kills players that touch this.collider.
using UnityEngine;
// Attach this to the grass collider
public class Ground : MonoBehaviour {
// Called when another collider hits the grass.
// This is part of Unity!
void OnCollisionEnter(Collision c) {
// Does the other collider have the tag "Player"?
if (c.gameObject.tag == "Player") {
// Yes it does. Destroy the entire gameObject.
Destroy(c.gameObject);
}
}
}
Now, I need to it transition to a game over overlay, which asks me to restart, yes or no.
The resources are out there in regards to learning Unity3D efficiently.
Look at the Unity3D tutorials: https://unity3d.com/learn/tutorials
These tutorials are usually kept up to date in terms of not using deprecated code. Additionally, several of the tutorials will teach you how to set up events like those that you need.
In regards to your immediate question though, look into forming the logic for your game.
You're going to need allow your player gameobject to fall via either gravitational force enacted on a rigidbody or hard-coded physics being applied to the gameobject. Next you will need to determine if the player gameobject has collided with the "floor". If so you will need to trigger an event to destroy the player gameobject, then display a GUI Text that says Game Over.
Look around more at tutorials and get better acquianted with Unity. Over time, you'll learn how to make things happen. If you have more questions, feel free to ask.
Answer to you update:
If your code is functioning correctly and your destroying your gameobject correctly, then awesome! You're learning fast!
The next step could be:
Create a Canvas, create a gui panel, create a gui text
That gui text object can have your Game Over Text
Now, create a button that you will utilize as the restart button
You can have the button call a function which utilizes SceneManager.LoadScene to reload the scene. Look here for an example: http://answers.unity3d.com/questions/1109497/unity-53-how-to-load-current-level.html
Next, Disable the panel as you will not need it until your player dies.
When your player collides with the ground you will destroy the player gameobject and set the panel you created to active. (you can trigger these actions via a listener in code or via the button component in the inspector).