- Home /
Call Other Object to Self
Hey Everybody So I am working on an FPS Shooter with unlockable weapons and what happens is when I unlock a weapon, it randomly places it on the level somewhere. Is there a script that I could attach to the Player to call the weapon to its position and rotation at the start of the level? Thanks!!!
(Also is there a way to add variables to that so I can make it a relative position to the player at the beginning of the level.. Does that make Sense?) Thanks in advance again!!
CLARIFICATION: Well to clarify, what happens is I have an unlocked weapon already that stays on each level DontDestroyOnLoad. I have a player named ASHOOTER (which is my player) What i wanted was a script that is a component of ASHOOTER that "calls" the weapon to its current position and rotation. In the same script, I wanted variables where in the inspector I could make it called to a position relative to the ASHOOTER. So for example if ASHOOTER is at (1, 1, 1). And in the inspector the relative values for x, y, and z were all -.5, then the weapon would get called to (.5, .5, .5)
Answer by Seth-Bergman · Aug 12, 2012 at 02:42 PM
you could attach this to your weapon:
var player : GameObject;
var xOffset : float = .5;
var yOffset : float = .5;
var zOffset : float = .5;
var xRotationOffset : float = 0.0;
var yRotationOffset : float = 0.0;
var zRotationOffset : float = 0.0;
function Start(){
player = GameObject.Find("ASHOOTER");
transform.position = Vector3(player.transform.position.x + xOffset,player.transform.position.y + yOffset,player.transform.position.z + zOffset);
transform.rotation = player.transform.rotation;
transform.Rotate(Vector3(xRotationOffset,yRotationOffset,zRotationOffset));
transform.parent = player.transform; // this childs the weapon to the player so it will stay with him...
}
it's basic, but this should work I think.. all the offsets will be visible in the inspector..
to fine tune the positions, you can change it from start to update, then change it back to start to save on performance (once you know what values to use)..
EDIT: tried to add rotation vars, I THINK that should work
that mostly works... how about variables for rotation?
lol, hmm, that's a bit trickier..
maybe an easy way would be:
transform.Rotate(Vector3(xRotationOffset,yRotationOffset,zRotationOffset));
if you call that just once (as in the start), I think it should work.. don't forget to set up the vars the same way... I edited above..
Answer by AnXgotta · Aug 12, 2012 at 03:56 AM
I'm not sure what you mean in your first question about making a script to attach to the player. The design is up to you. Maybe clarify that a bit.
For your second question what I think you are asking is can you have the unlocked weapon spawn in a random place relative to the current player position. If this is the case you could do this a number of ways.
1) You could use the transform to get a random position with a max X distance from the player of maxX and max Z position maxZ (assuming you use X and Z coords for the 2d plane of the ground)
Vector3 randSpawnPos = new Vector3(playerTransform.x + Random.Range(-maxX, maxX), playerTransform.y /*(or whatever height you want)*/, playerTransform.z + Random.Range(-maxZ, maxZ));
2) You could use a Random.insideUnitCircle to get a random position in a circle around your player then use a random value for the distance from your player
float radius = Random.Range(minRadius, maxRadius);
Vector2 randPos = Random.insideUnitCircle * radius;
You could set the radius to a constant or randomly get one like coded above.
Hopefully this is helpful.
Edit: Sorry, I misunderstood your question.
Well to clarify, what happens is I have an unlocked weapon already that stays on each level DontDestroyOnLoad. I have a player named ASHOOTER (which is my player) What i wanted was a script that is a component of ASHOOTER that "calls" the weapon to its current position and rotation. In the same script, I wanted variables where in the inspector I could make it called to a position relative to the ASHOOTER.
So for example if ASHOOTER is at (1, 1, 1). And in the inspector the relative values for x, y, and z were all -.5, then the weapon would get called to (.5, .5, .5)
basically just say:
var weapon : GameObject;
var player : GameObject = GameObject.Find("ASHOOTER");
weapon.transform.position = player.transform.position;
as for adjusting it, you could say something like:
var weapon : GameObject;
var player : GameObject = GameObject.Find("ASHOOTER");
weapon.transform.position = Vector3(player.transform.position.x + xOffset,player.transform.position.y + yOffset,player.transform.position.z + zOffset);
unknown identifiers xOffset, yOffset, zOffset. and that would be attached to my weapon? or player...
those would be your relative positions, as described above..
you need to declare them:
var xOffset = .5;
etc..
in my above example, you would also need to set the var "weapon" to reference the correct weapon, which I did not do..
give me a $$anonymous$$ute to elaborate...
Oh i understand I think. wait but can I just make xOffset, yOffset, zOffset all variables so i can edit them in the inspector?
Your answer
Follow this Question
Related Questions
make player move in direction it's facing 2 Answers
Game Object won't match rotation of new positions transform 1 Answer
Camera rotation around player while following. 6 Answers
Reset posiotion 1 Answer
Saving System? 1 Answer