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
2
Question by ik_jolan · Dec 02, 2013 at 08:50 PM · key pressed

spawn object on keypress c#

Hello, I'm trying to spawn an object in front of the player when pressing on 'X' I'm very new to c# and don't now how to do this could anyone help me with this?

Comment
Add comment · Show 5
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 Seth-McCumber · Dec 02, 2013 at 09:13 PM 0
Share

Heres some code that may or may not work, not tested -

 using System.Collections;
 using UnityEngine;
 
 public class SpawnOBJ : $$anonymous$$onoBehaviour {
 
     public Vector3 ObjectSpawnPosition;
     public GameObject obj;
 
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
         Instantiate(obj, ObjectSpawnPosition, Quaternion.identity) as Transform;
     }
 }
avatar image ik_jolan · Dec 02, 2013 at 09:57 PM 0
Share

Assets/Resources/NewScripts/spawnmedc.cs(9,6): error CS1519: Unexpected symbol `if' in class, struct, or interface member declaration

does not work...

avatar image The Kracken · Dec 02, 2013 at 09:59 PM 0
Share
 using System.Collections;
 using UnityEngine;
  
 public class SpawnOBJ : $$anonymous$$onoBehaviour {
  
     public Vector3 ObjectSpawnPosition;
     public GameObject obj;
 
 void Update(){
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
        Instantiate(obj, ObjectSpawnPosition, Quaternion.identity) as Transform;
     }
 }
 }

That should fix it.

avatar image Seth-McCumber · Dec 02, 2013 at 10:21 PM 0
Share

Woops, messed up there

avatar image flaviusxvii · Dec 02, 2013 at 10:28 PM 0
Share

Tags like "help..." and all variants thereof are USELESS on a site devoted to giving people help. Put a little more thought into your tags so those that come later find relevant information.

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by The Kracken · Dec 02, 2013 at 09:31 PM

 if(Input.GetKeyDown(KeyCode.X)) {
        Instantiate(obj, ObjectSpawnPosition, Quaternion.identity);
     }

Try That.

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 ik_jolan · Dec 02, 2013 at 09:54 PM 0
Share

Thanks! it does spawn only, I have to add the coords now, is there a way to spawn it in front of the player?

avatar image The Kracken · Dec 02, 2013 at 09:57 PM 1
Share

The ObjectSpawnPosition should be where you want it to spawn. one way to do it is to create an empty GameObject and parent it to your player. then position it where you want it to spawn and then drag it into the inspector.

Another soulution would be

 ObjectSpawnPosition = Player.transform.position + (Player.transform.forward * distance);

where distance is how far in front of the player you want it to spawn.

avatar image
0

Answer by Papayroos · Oct 01, 2017 at 04:41 AM

@The Kracken How do you spawn it at a velocity?

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

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

20 People are following this question.

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

Related Questions

is it possible to make the player to transform into a differet object? 0 Answers

Equip in-game map item 1 Answer

Make a rigidbody sphere stop while moving fast 2 Answers

Grapple gun help 1 Answer

how do I put different color on cubes that are spawning? 0 Answers


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