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 AdmanX · Sep 22, 2012 at 10:20 PM · destroykeyboardgame object

Destroying game object next to player

Hi i am trying to create a script that destroys a game object that is directly next to my player with a keyboard command.

My game has a 2d perspective and a third person view with the camera locked to following my player who is controlled by a character controller ( i guess similar to old mario games) what i am aiming for is - by pushing a direction (left, up, right or down) and the "k" key you destroy the game object adjacent to the player in that direction

the current script i have is destroying game objects when i click but i can't figure out how to get it to work the way i want. I have tried changing the input method to (for eg) "K" but it still has no effect

the code i have is

 function Update(){
   if (Input.GetMouseButtonDown(1)){
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit: RaycastHit;
     if (Physics.Raycast(ray, hit)){
       Destroy(hit.transform.gameObject);
     }
   }
 }

Any help would be greatly appriciated

Comment
Add comment · Show 2
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 AlucardJay · Sep 25, 2012 at 10:09 AM 0
Share

This is an interesting idea, but the question needs alot more information to get help.

Is this 2D or 3D? How is the player character viewed? How does the player control the player and camera? How is it shown where the player is choosing to dig?

Your destroy example suggests you are using a mouse to choose what to dig, with the question being how do I choose an object immediately around the player character.

Your text suggests you want to have the player character dig directly in front on a certain keypress, down on another keypress, left with another etc. Please clarify and edit the question. A screenshot of the player character in the environment as seen while playing would be great.

avatar image AdmanX · Sep 25, 2012 at 10:23 AM 0
Share

Thanks for the advice i have updated post

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by refardeon · Sep 25, 2012 at 10:35 AM

Can't you use the player characters location as source for your ray? Something like:

 if (rightButton) {
    var ray = new Ray ( player.transform.position, Vector3.right, distance);
 } else if (downButton) {
    var ray = new Ray ( player.transform.position, -Vector3.up, distance);
 }
 // Same for left (-Vector3.right) and up (Vector3.up)
 
 var hit: RaycastHit;
 if (Physics.Raycast(ray, hit)){
    Destroy(hit.transform.gameObject);
 }
 

This is very untested, but something along these lines should work.

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 AdmanX · Sep 25, 2012 at 10:39 AM 0
Share

Thanks refardeon i'll give it a go this evening. :)

avatar image refardeon · Sep 25, 2012 at 10:40 AM 0
Share

You're welcome!

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

11 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

Related Questions

Script to destroy object 1 Answer

OnTriggerEnter not working 1 Answer

destroy parent of script 1 Answer

Destruct gameobject by call from another script 1 Answer

Destroyable terrain? 2 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