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 SgtScrambles · Sep 21, 2012 at 02:21 PM · simplepick up

Simple Object Pick-Up

I am making a game where your only goal is to cause havoc. Right now I have a script where I am able to pick up and move around objects. The only problem is that I pick up all the objects instead of just one.

`var other : Rigidbody;

function Update () { if(Input.GetButtonDown("Fire1")) { other.transform.parent = transform;

 }else if(Input.GetButtonUp("Fire1")) {
     other.transform.parent = null;
 }

} `

Comment
Add comment · Show 4
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 FatWednesday · Sep 21, 2012 at 03:30 PM 0
Share

is this code on all of the objects? what is other in this context and how/when is it set? need more information to fully understand the problem here.

avatar image AMDAndrew · Sep 21, 2012 at 03:44 PM 0
Share

You have to make 2 scripts, 1 for the object you want to pick up and 1 for picking up.

Ex :

Object script -> transform script If player presses fire moving becomes avabile.

Player script -> Pickup script If you press fire moving object with tag becomes avabile

avatar image aballif AMDAndrew · Apr 21, 2016 at 07:19 PM 0
Share

I have done it without the Player script. Jimmy Vegas has a tutorial where he picks up and object with only one script.

avatar image Mander · Sep 21, 2012 at 03:47 PM 0
Share

ok. there are some ways of doing this. it all depends on how ur game works.

are u picking the objects if u get close or if u targe them with the mouse? how?. this would greatly change the script

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by john-essy · Sep 21, 2012 at 04:00 PM

If you are picking up objects i would use a RayCast Then within your update method i would do something like

 if (Physics.Raycast (transform.position, Vector3.forward, hit, 100.0))`
     {
         if(hit.collider.gameObject.tag == "PickUpableObject")
         {
         hit.transform.parent = transform;
         }
     else
     {
         hit.transform.parent = null;
     }
 }


This way only what ever the ray hit will be picked up.

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
avatar image
0

Answer by SgtScrambles · Sep 24, 2012 at 02:04 PM

The code is on the Main Camera. I tried using a RayCast but I kept getting errors like "Assets/Script/PickUpObject.js(5,25): BCE0023: No appropriate version of 'UnityEngine.Physics.Raycast' for the argument list '(UnityEngine.Vector3, UnityEngine.Vector3, UnityEngine.Rigidbody, float)' was found." I havent tried a GetComponent and I'm not sure how I would exactly do that.

Comment
Add comment · Show 1 · 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 john-essy · Sep 24, 2012 at 02:38 PM 0
Share

To get a component you would need to reference what you want so say i have a player GameObject in the scene and he is able to move around and do all the things a player would do. But then say i need to be able to access his health this is where i would need to get a component "His Health" so i would do

public PlayerHealth health;

PlayerHealth would be the name of your script you are trying to access. Then in start i would find it

void Start() { health = GameObject("Player").GetComponent(); }

Now i am able to access his health just by dong this

health -= a certain amount.

As for your error you will need to create a Ray or Raycasthit

So you will need to do something like

RaycastHit hit; above the code i provided below this will give you access to there ray class.

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

14 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

Related Questions

Whats wrong with this simple code? 2 Answers

List.Contains problem 1 Answer

Glitching between box colliders. One is kinematic... Help! 1 Answer

Why collidion doesn't work on a cube attached to first person controller 1 Answer

Simple Topdown Movement Problem 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