- Home /
The question is answered, right answer was accepted
Transform movement going through Objects?(JS)
So I have a space themed seen set up and I have a big long script working really well, but im having troubles with it. Basically the ship just goes right through everything and ignores colliders of any type. Im using: transform.Translate (Vector3(0,0,1) * Time.deltaTime*speed); I was wondering if theres a alternative in JavaScript Any help would be greatly appreciated thanks!
Answer by GlatiatorRX · Oct 06, 2015 at 03:47 PM
I have not used Javascript in a little bit. But the reason why your objects are most likely clipping is because using colliders do not mean there will actually be a reaction to the object. Simply translating an object will basically force it to a position, regardless if there is an object already there.
If you add a rigid-body and add a force, velocity, or impulse in a certain direction, instead of doing a translation, it should work better for you. I can't really say how to do that in javascript, but here is a reference. http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Either do that, or you have to do your own computations to check if you are colliding and hence stop your ship from moving in said direction. Like do an on collision check and then restrict your movement along an axis, or some other direction.
Basically what @taxvi commented above should work too.
Answer by taxvi · Oct 06, 2015 at 03:27 PM
well, you can find it here, here, here, here and here unfortunately this means that this question is a spam :|
I looked through each of these and none helped. I have colliders on both objects and I don't have a way of referencing a character controller
well, ok, what transform.Translate() does is it teleports your game object each frame. it does not care about colliders or any forces. never move transform anywhere if you want to apply some physics. what you need to do is attach a rigidbody to the object that is going to move. then reference that rigidbody in your script. then use rigidbody.$$anonymous$$ovePosition() to move your object. rigidbody is able to consider all the colliders around it and collide with them ins$$anonymous$$d of going through them. and do this in FixedUpdate.
Follow this Question
Related Questions
Tilemap Collider 2D preventing objects from moving 2 Answers
Does disabling colliders increase performance? 2 Answers
Why there is no Collider.IsTouching(...) ? 2 Answers
OnTriggerEnter does not work 1 Answer
Inconsistent Collision Detection 3 Answers