x


Freeze movement of game object to one direction(Horizontal)

I'm using following script to move gameobject when player push it, But i want that my gameobject should only move in one (horizontal) direction, as now it can move to any direction even it bounces if player jump while pushing it.

JS: var pushPower = 2.0; function OnControllerColliderHit (hit : ControllerColliderHit) { var body : Rigidbody = hit.collider.attachedRigidbody; if (body == null || body.isKinematic) return; if (hit.moveDirection.y < -0.3) return; var pushDir : Vector3 = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z); body.velocity = pushDir * pushPower; }

more ▼

asked Dec 05, 2012 at 02:50 PM

himanshu619 gravatar image

himanshu619
59 20 19 22

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

In the other object's rigid body, check freeze z and y.

The trick only works for rigidbodies, only affects velocity and force (it won't stop you from changing position z and y directly) and only woks for global x,y z axises (you can't turn it then lock it the new, diagonal, x.)

more ▼

answered Dec 05, 2012 at 03:00 PM

Owen Reynolds gravatar image

Owen Reynolds
23.2k 3 18 81

Thanks for your reply, I had already freeze the gameobject's angular rotation. But when i was freezing the axis it was not moving in any of the direction, Please suggest something else.

Dec 06, 2012 at 04:11 AM himanshu619

Freezing xyz in the Inspector (just not all three) definitely works in general. I've used and seen it used plenty of times. For a test, have a ball roll diagonal down a hill, or knock into another to send it diagonal. Then lock a single axis. It's moves the same way, with that axis removed.

Dec 06, 2012 at 03:51 PM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left

I actually had stumbled across something for my last project relating to this.

It should work with anything, not only rigidbodies.

 var lastLegalLoc : Vector3;
 var zConstraint : float;
 
 function Start(){
 
     lastLegalLoc = transform.position;
     zConstraint = transform.position.z;
 
 }
 
 function Update(){
 
     if(transform.position.z == zConstraint) lastLegalLoc = transform.position;
     else transform.position = lastLegalLoc; 
 
 }
more ▼

answered Dec 06, 2012 at 04:38 AM

Alismuffin gravatar image

Alismuffin
161 59 51 52

I have a feeling there is a bit more missing from this, but IO can't find where I got the idea anymore :c

Dec 06, 2012 at 04:38 AM Alismuffin

Dear it's not working for me!

Dec 06, 2012 at 01:30 PM himanshu619

The update code says to completely cancel any movement that changes z (and even pure x movement will change z by 0.00001.) So a regular RB never moves. To let it move, but only cancel the z part, set `transform.postion.z` to the starting z.

Dec 06, 2012 at 03:43 PM Owen Reynolds

Can you suggest any other object coz' its not working for me!

Dec 12, 2012 at 04:13 PM himanshu619
(comments are locked)
10|3000 characters needed characters left

Why Dont you freeze the Rotation From Inspector?

more ▼

answered Dec 06, 2012 at 05:22 AM

Maulik2208 gravatar image

Maulik2208
1.1k 36 33 44

Because although it is frozen, it merely means the player cannot move or rotate the object in question in particuklar axis. If you bumped into a cylindrical collider however, the object will slide to the side of it creating an unwanted offset

Dec 06, 2012 at 05:35 AM Alismuffin
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3697
x2617

asked: Dec 05, 2012 at 02:50 PM

Seen: 1489 times

Last Updated: Dec 12, 2012 at 04:13 PM