Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by chuggy25 · Jun 15, 2016 at 06:21 AM · gameobjectspawningtranslateroation

Is there a script that allows you to spawn a game object a few meters in front of your FPS controller.

I can't seem to make a script that spawns my game object at the right height, rotation, and direction. I need it to spawn a meter in front of my character, and level (or close to level) with the ground. I can get as far as spawning the object directly to the right of my player, but it is half underground and doesn't take into account the direction my player is facing. Any help is appreciated!

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

Answer by jdean300 · Jun 15, 2016 at 07:13 AM

 //"distance" meters in front of this object
 var distance = 1f;
 var pos = transform.position + transform.forward * distance;
 
 //Facing this object
 var rot = transform.rotation * Quaternion.Euler(0, 180f, 0);
 
 var obj = (GameObject) Instantiate(ObjectPrefab, pos, rot);
 
 //Find the renderer on this object and get its bounds (now we know how tall it is)
 //alternatively, if you have a collider that is larger than the rendered bounds,
 //you could get the colliders bounds
 var bounds = obj.GetComponent<Renderer>().bounds;
 
 //Now move the object up/down to be level with the ground
 var distFromGround = bounds.min.y - groundYLevel;
 obj.transform.position -= new Vector3(0, distFromGround, 0);

Comment
Add comment · Show 2 · 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 EpiFouloux · Jun 15, 2016 at 07:38 AM 0
Share

maybe add a coefficient to the position ( pos = transform.position + transform.forward * distance ) to show where to distance management is.

avatar image jdean300 EpiFouloux · Jun 15, 2016 at 07:40 AM 0
Share

good point, thanks

avatar image
0

Answer by chuggy25 · Jun 15, 2016 at 05:46 PM

Thanks for your help guys, As I am new to this I don't really know how to tailor this to my own use, so I was forced to add the few c# variables I know how to work with. Below is the edited script, and a picture of the game object spawn location when using the attached script.![alt text][1]


error.jpg (248.6 kB)
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 chuggy25 · Jun 15, 2016 at 08:43 PM 0
Share

Sorry for the wonky code using UnityEngine; using System.Collections; using System;

 public class Box : $$anonymous$$onoBehaviour {
     public GameObject GameObject;
     public GameObject player;
     
     // Use this for initialization
     void Start () {
     
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.Get$$anonymous$$ouseButtonDown(0))
             GameObject.transform.position = player.transform.position + transform.forward * 5;
 
        //Facing this object
             var rot = transform.rotation * Quaternion.Euler(0, 180f, 0);
 
             var obj = (GameObject);
         //Find the renderer on this object and get its bounds (now we know how tall it is)
         //alternatively, if you have a collider that is larger than the rendered bounds,
         //you could get the colliders bounds
         var bounds = obj.GetComponent<Renderer>().bounds;
 
         //Now move the object up/down to be level with the ground
         var distFromGround = bounds.$$anonymous$$.y - 12;
             obj.transform.position -= new Vector3(0, distFromGround, 0);
 
 
avatar image jdean300 · Jun 15, 2016 at 08:49 PM 0
Share

The only thing I can see that might be an issue is you probably want var rot = player.transform.rotation * Quaternion.Euler(0, 180f, 0);

I can't really tell what the issue is from the picture, you need to give a better explanation.

avatar image chuggy25 jdean300 · Jun 16, 2016 at 03:04 AM 0
Share

The error is that the game object (black, lined cube ) is appearing to the right, and below my player. I need the cube to appear in front of the player, and always be facing the him/her. Someone suggested a ray-cast script might be an easier way to do it. But this Is what I've got at the moment.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How would you create a 2D endless runner with objects moving in the background? 0 Answers

Prefab to automatically follow a waypoint path? 1 Answer

How can I have prefab spawned at the game's start and at a position? 0 Answers

GameObject doesn't move 1 Answer

How can I instantiate a object on the world coordinates 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