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 caiodmk · Oct 11, 2011 at 10:34 PM · c#grabhold

Grabing and moving a box(C#)

I´m working on a game where the player has to solve some puzzles by moving boxes around, He has to grab(hold) them and place at the right place. I have my character moving around(3rd person), he moves forward, backwards and strafes to both sides, the mouse controls the camera like in World of Warcraft. What i need to know now is how can i make it grab the box by pressing a button and move it around with him, only releasing the box when i release that button. Thanks

codes need to be in C# pls

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

3 Replies

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

Answer by DaveA · Oct 11, 2011 at 10:58 PM

Reparent the box to the player (or some part of the player)

 obj.transform.parent = player;

or something like that. To release, just set the parent to null.

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 caiodmk · Oct 16, 2011 at 06:05 AM

"Reparent the box to the player (or some part of the player)

obj.transform.parent = player;

or something like that. To release, just set the parent to null."

I'm not sure how do this, what I tried to do is

 using UnityEngine;

class MoveBox : MonoBehaviour {

 void OnCollisionStay(Collision col)
 {
     if (col.gameObject.tag == "Player" && Input.GetKeyDown(KeyCode.E))
     {
         this.transform.parent = GameObject.FindWithTag("Player");
     }
     else
         this.transform.parent = null;

 }

}

Doesn't work tho..I get the error "Cannot inplicitly convert type 'UnityEngine.GameObject' to'UnityEngine.Transform'"..

And I also wanted the code to be on my player(otherwise I'll have to add the script to every single box..and there will be many), but I couldn't find a way to get it to work. Thats my player script:

 using UnityEngine;

class Move : MonoBehaviour // Sempre que houver a necessidade de mexer na cena tem de herdar a classe MonoBehavior { public int vel = 5; // Se não colocar nada o programa entende a variavel como privada public Transform spawPoint; public Transform focoVisao;

 public void Update()
 {
     //transform.LookAt(focoVisao);
     transform.Translate(vel * Time.deltaTime * Input.GetAxis("Horizontal"), 0, vel * Time.deltaTime * Input.GetAxis("Vertical")); // Time.deltaTima representa o tempo de resposta entre os frames
 }
 
 public void OnCollisionEnter(Collision hit)
 {
     if(hit.gameObject.tag == "rochas")
     {
         transform.position = spawPoint.position;
     }
     
 }


 

}

I'm still trying to find a way to solve the "LookAt" problem (so it doesn't move on the Y axis).. help pls

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 syclamoth · Oct 16, 2011 at 06:09 AM 1
Share

Well, as for the adding lots of scripts to lots of boxes, that's kind of what prefabs are for.

Also, you could just write your own lookat script? Which only acts in two dimensions? Just remove the y component from everything, and then just use normal trig and Quaternion.EulerAngles to rebuild the rotations. $$anonymous$$athf.Atan2 should do the trick. (as long as you convert things back to degrees at the end- Atan2 returns radians)

avatar image caiodmk · Oct 16, 2011 at 06:52 PM 0
Share

Thanks @syclamoth, unfortuntelly I don't have enough knowledge on C# to write that code, could you please help me with some? I've been trying to make it work for a long time and can't get it =/..Can you help me with some code pls? or point me to somewhere where i can learn about it. Usually stuff I find is a bit hard to understand as I don't know much about C# yet. Thanks

avatar image syclamoth · Oct 17, 2011 at 02:16 AM 1
Share

Oh, as for the first problem, GameObject.FindWithTag returns a gameObject, not a Transform! However, every gameObject also has a transform attached to it, so you can just use

 GameObject.FindWithTag("whatever").transform

for anything that needs that ins$$anonymous$$d.

avatar image
0

Answer by caiodmk · Oct 17, 2011 at 12:34 AM

Thanks @syclamoth, unfortuntelly I don't have enough knowledge on C# to write that code, could you please help me with some? I've been trying to make it work for a long time and can't get it =/..Can you help me with some code pls? or point me to somewhere where i can learn about it. Usually stuff I find is a bit hard to understand as I don't know much about C# yet. Thanks

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Refering to gameobject script is attached to 1 Answer

Picking a Random Order for Levers 1 Answer

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System. 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