How do I offset the Particle System relative to Ship

Hi,

I have assumed the same method of attaching a particle system to the Player Ship, as another student on here. I used a Script on the particle system. What this does is ensure the transforms are directly over each other. However, if I try to say, set a Vector 3 startPosition = New Vector 3 (0, -4, 0) and put start postion in place of the playership.transform position, then I get an error.

I feel my logic is on the right path, but I am missing something fundamental here. Please help!!!

The script below doesn’t display any attempt to do this, so this code currently plops the sets the particle system as directly the same as the player ship.

	private PlayerController playerShip;
	private Vector3 shipPosition;

	// Use this for initialization
	void Start () {
		playerShip = GameObject.FindObjectOfType<PlayerController> ();
	}
	
	// Update is called once per frame
	void Update () {
		GetThrusterPosition ();
	}

	void GetThrusterPosition(){
		shipPosition = new Vector3 (playerShip.transform.position.x, playerShip.transform.position.y);
		//shipPosition = playership.transform.position
		this.transform.position = shipPosition;
	}
}

What is the error you are getting? If it relates to this;

“Vector 3 startPosition = New Vector 3 (0, -4, 0)”

from your above post, then it needs to be;

Vector3 startPosition = new Vector3(0, -4, 0);

Also, if you always want the position of the ParticleSystem to be slightly offset from the player ship, why not just adjust it’s transform within the Inspector in the first place? For example, if it was for an engine burn/thrusters, they will presumably always be at the back of the ship.

Crikey Rob, like a fly on… I wont be able to put that up until later, I’m at work. Thanks for the swift reply. Would you expect something like this below to work?
Anyway, ill add more detail later on. Thanks.

void GetThrusterPosition()
{
    Vector3 newPostion = this.transform.position + new Vector3 (0, -0.4, 0);
    shipPosition = new Vector3 (playerShip.transform.position.x, newPosition, 0);
    //shipPosition = playership.transform.position
    this.transform.position = shipPosition;

I apologise I didn’t see all of your reply Rob before adding my first reply.

Perhaps I have misunderstood something here. I am using a piece of code to set the transform position of the thrust. Would that not therefore overwrite any transform position set in the Inspector?

I should also point out, that my ship moves in the Y dimension as well. Which is therefore leaving me with extra complications. I think I have managed to have an offset thruster before, but when moving the ship up the screen, the thrust remained in position.

Hi,

Couple of things;

  • missing closing brace, but I suspect that was just whilst typing it for the conversation, not from your project
  • the Vector3 constructor requires floats, not ints or doubles, you’ll get an error with that for sure

Perhaps I have misunderstood something here. I am using a piece of code to set the transform position of the thrust. Would that not therefore overwrite any transform position set in the Inspector?

If your player ship is a GameObject, and you then added another GameObject to that one, for the thrust (ParticleSystem), e.g. the thrust is nested under the ship, then the thrust will inherit its parent’s position;

image

If you then set the y component of the thruster’s transform to -0.4f this would be offset against the position of it’s parent;

image

If you want the particles to appear beneath the ship, set the x component of the thruster’s transform to 90f, if you don’t your particles will be going into the screen (turn 2D mode off to see this in effect);

image

image

In the above, no additional script was required.

I apologise I didn’t see all of your reply Rob before adding my first reply.

No worries, I actually went back in and updated it, so we probably overlapped :slight_smile:


See also;

Hi Rob, sorry this has taken so long to reply. I guess I know I could use the child/parent method you described above, but I am also trying to learn to code, and wanted to achieve this programmatically.

So, this code below as far as I understand it:

Gets the PlayerController and sets it to playership.
Then set shipPosition to be the same as shipThrust (this.transform.position), which is the game object for the particle system.

Maybe I am trying to run before I walk here. The code works perfectly well if “playership.transform.position.y” replaces “offset” in the Vector, but obviously the particle system has the same transform position as the ship.

The error I get with the code as below is:
The best overloaded match for "UnityEngine.Vector3(float, float) has some invalid arguments.

public class shipThrust : MonoBehaviour {

	private PlayerController playerShip;
	private Vector3 shipPosition;

	// Use this for initialization
	void Start () {
		playerShip = GameObject.FindObjectOfType<PlayerController> ();
	}
	
	// Update is called once per frame
	void Update () {
		GetThrusterPosition ();
	}

	void GetThrusterPosition(){
		Vector3 offSet = new Vector3 (0f, -0.5f, 0f);
		shipPosition = new Vector3 (playerShip.transform.position.x, offSet);
		//shipPosition = playership.transform.position
		this.transform.position = shipPosition;
	}
}

I appreciate I could be a mile off here :slight_smile:, and may well just get rid of the shipThrust script and do it the other way! (To confirm, shipThrust is attached to the particle system).

Thanks for all your help Rob, I hope you get paid for all this haha :wink:

FINALLY worked it out, I was just doing it the wrong way around.

	void GetThrusterPosition(){
		shipPosition = new Vector3 (playerShip.transform.position.x, playerShip.transform.position.y);
		this.transform.position = shipPosition;
		this.transform.position = this.transform.position + new Vector3 (0f, -1f, 0f); 
               //this sets the particle system transform AS player ship position, then adds an offset
	}
}

You’re more than welcome Andy, my apologies for the delay responding.

The invalid argument error, as you have probably realised by now, would have been because you were trying to pass a Vector3 as an argument for another Vector3, where-as it’s constructor requires floats to be used.

Glad you have this resolved now :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms