- Home /
Placing particle relative to character code
Hey guys. I have created code to find specific points around the camera, and I need to create a particle relative to the camera on update. I need the code to update every frame however, so the code can not be too memory intensive. Any ideas on how this is possible?
I am going to clarify things a lot here. So what I am trying to do is to move a particle along a selected line calculated by code. This happens when the mouse is clicked and then goes through some calculations to find where the particle needs to be. The particle needs to be relative to the main camera, but moving to specific parts inside of the relative area.
Answer by Uldeim · Nov 06, 2014 at 08:01 PM
Your question isn't very clear, but what I understand from it is that you're looking to have a "particle" (by which you mean GameObject or ParticleSystem, I presume) stay in the same position relative to the camera regardless of when or where the camera moves.
If that's correct, the simplest solution seems to me to be to make the "particle" a child of the Camera object if that's possible. That will mean that you don't have to do any of the work yourself; Unity will take care of it for you.
You can make a GameObject a child of another by either selecting it in the Hierarchy and dragging it over the Camera there, or in script by using something like:
particle.transform.parent = MainCamera.transform;
If you can't (for whatever reason) make the "particle" a direct child of the Camera, you could instead use an Empty Game Object. Then, on Update, simply assign the position of the "particle" to be the position of the EGO.
That is not quite the issue. I have the code for where the particle is being transformed to now. $$anonymous$$y problem is creating the particle itself. I have some code put in but get an error message:
NullReferenceException: Object reference not set to an instance of an object SwordAttack.Update () (at E:/Game Stuff/Zypher/Assets/Code/Player Code/SwordAttack.cs:60)
The code is:
class SwordAttack : $$anonymous$$onoBehaviour{
ParticleSystem Sword_Line ;
void Update()
{
Sword_Line.transform.Translate (swordpoint.x, swordpoint.y, swordpoint.z, Camera.main.transform);
Sword_Line.emissionRate = 10;
}
void start(){
Sword_Line.emissionRate = 0;
SwordLine.position = new Vector3(0, -5, 0);
SwordLine.color = new Color(0f, 0f, 1f);
SwordLine.size = 0.1f;
}
}
I have cut out some of the extra code but line 60 is: Sword_Line.transform.Translate (swordpoint.x, swordpoint.y, swordpoint.z, Camera.main.transform);
It seems that I am not setting all of the variables in the code but I cannot find what I am missing.
P.S. some of the extra code includes an if statement so it does not create a particle every update.
Ok, well, that means that either swordpoint or Camera.main is null when Update() is called. Try throwing in a couple of Debugs:
Debug.Log("Swordpoint is " + (swordpoint==null ? "null!" : "not null."));
Debug.Log("Camera.main is " + (Camera.main==null ? "null!" : "not null."));
If, as I suspect, swordpoint is occasionally null, wrap that section in a
if (swordpoint != null)
{}
block.
I just tested it and neither the Swordpoint or the Camera.main is null. I think it is someting with the Sword_Line that is not properly initiated. I cannot find out what it is though.
Hi, Sword_Line is a ParticleSystem, it inherits from Component and then needs to be attached to a GameObject like other Components. I think you may have to add the ParticleSystem Component you designed to your Sword object and get access to it by GetComponent if you need to access it's properties.
Alright, so I am working on importing it from assets but am still having issues. First off here is the code I have now:
class SwordAttack : $$anonymous$$onoBehaviour{
public Particle Sword_Line;
void Update()
{
Sword_Line.position = new Vector3(swordpoint.x, swordpoint.y, swordpoint.z, Camera.main.transform);
}
void start(){
Sword_Line = gameObject.GetComponent("SwordLine") as Particle;
}
}
Basically I am down to only 2 few errors now. First off, is the GetComponent code does not like the "as Particle" part. It comes up with the message:
Assets/Code/Player Code/SwordAttack.cs(59,67): error CS0077: The as' operator cannot be used with a non-nullable value type
UnityEngine.Particle'
And then after that, it does not like the .position code because of the 4th statment. I am not sure how to fix that one at all.
Thank you everyone for working with me thus far. -Z
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Full screen camera missing? 3 Answers
how do i make first person character rotate left and right along with camera? 0 Answers