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 sketchers1 · Aug 11, 2012 at 07:24 PM · rotationpositionplayerweaponcall

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)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 3 · 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 sketchers1 · Aug 12, 2012 at 02:52 PM 0
Share

that mostly works... how about variables for rotation?

avatar image Seth-Bergman · Aug 12, 2012 at 03:03 PM 0
Share

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..

avatar image sketchers1 · Aug 12, 2012 at 03:58 PM 0
Share

hey it works! Thanks again!

avatar image
0

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.

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 sketchers1 · Aug 12, 2012 at 02:13 PM 0
Share

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)

avatar image Seth-Bergman · Aug 12, 2012 at 02:23 PM 0
Share

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);
 
avatar image sketchers1 · Aug 12, 2012 at 02:33 PM 0
Share

unknown identifiers xOffset, yOffset, zOffset. and that would be attached to my weapon? or player...

avatar image Seth-Bergman · Aug 12, 2012 at 02:38 PM 0
Share

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...

avatar image sketchers1 · Aug 12, 2012 at 02:41 PM 0
Share

Oh i understand I think. wait but can I just make xOffset, yOffset, zOffset all variables so i can edit them in the inspector?

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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