Hello:
I’m making a 3D game. Using the FPS Character premade assets from unity.
What I’m trying to do is pulling/pushing one object (let’s say cube) but in one axis, this is: is not possible to push/pull it in 360 degrees.
Of course, you only would have 4 possible sides to push/pull the object. In consequence, when you pick the side (pressing a button), the camera should be focus to the direction to which you will push/pull the object. In this situation, you only can move up (push) or down (pull), not right/left, up-right, etc.
It’s like in ICO (the video game)
I tried to do something like this (attached to your player, thingMove would be the cube):
void OnTriggerStay(Collider other) {
this.transform.Rotate(0.0F,0.0F,0.0F);
thingMove.transform.position = new Vector3(this.transform.position.x, thingMove.transform.position.y, this.transform.position.z);
}
Trying to fix the rotation, etc. But is not working.
Could you recommend how to do it?
Thanks
Possible solution: let the player push the cube with physics. On the cube, in FixedUpdate, look at its velocity. If velocity is nonzero, figure out which of the three (or two?) axes is larger, and then eliminate the other axis/axes.
void FixedUpdate() {
//assuming XZ movement only for this.
if (rigidbody.velocity != Vector2.zero) {
if (rigidbody.velocity.x > rigidbody.velocity.z) rigibody.velocity = new Vector3(rigidbody.velocity.x, 0f, 0f);
else if (rigidbody.velocity.z > rigidbody.velocity.x) rigibody.velocity = new Vector3(0f, 0f, rigidbody.velocity.z);
}
}
There are two basic ways to do this.
- by using Physics, and locking movement to the direction that you want to go. (direction can be specified to one side or another or some such.)
- by using math where you move the block at the speed and direction of the character pushing it.
Both ways you can “sense” what general direction a character is from your target. (assuming you are not using triggers)
Vector3 pos = transform.InverseTransformPoint (player.position);
bool forward = Vector3.Dot (pos, Vector3.back) > 0.5f && Vector3.Dot(player.forward, transform.forward) > 0.5f;
bool back = Vector3.Dot (pos, Vector3.forward) > 0.5f && Vector3.Dot(player.forward, transform.back) > 0.5f;
bool left = Vector3.Dot (pos, Vector3.right) > 0.5f && Vector3.Dot(player.forward, transform.left) > 0.5f;
bool right = Vector3.Dot (pos, Vector3.left) > 0.5f && Vector3.Dot(player.forward, transform.right) > 0.5f;
it reads… if you are in the forward arc and facing the forward direction.
So basically you get only one of them to say true as an overall, you could do a distance test, or if you are using physics, a OnCollisionEnter/OnCollisionExit, or so to define where and when you could push or pull something.
1 Like