- Home /
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
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.
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
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)
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
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.
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