Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zypherr7 · Nov 06, 2014 at 07:27 PM · c#cameraparticles

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zypherr7 · Nov 10, 2014 at 09:21 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zypherr7 · Nov 06, 2014 at 08:38 PM 0
Share

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.

avatar image Uldeim · Nov 06, 2014 at 11:20 PM 0
Share

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.

avatar image Zypherr7 · Nov 07, 2014 at 03:59 PM 0
Share

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.

avatar image _dns_ · Nov 07, 2014 at 04:24 PM 0
Share

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.

avatar image Zypherr7 · Nov 07, 2014 at 05:25 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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

How to use Android Accelerometer(Left and right tilt) to turn a sphere object and camera in that direction? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges