- Home /
Moving object with transform.position ignore other objects even if they collided
Please give me help:-|
The Problem is Moving Sphere with changing "transform.position" ignore other objects even if they collided.
And now, I must use "transform.position" for some reason.
I'm coding my first game project with Unity, and I want Sphere to move along with bezier curving.
To realize this, I solved by changing gradually Sphere's "transform.position" value with bezier curving logic. And it successfully worked.
But, there is a big problem in this code... :(
Although, collision can be detected between moving Sphere and other game objects in OnCollisionEnter, the Sphere ignore other object and pass through destination as if there aren't any other objects in the orbit..
For Example, I coded like below.This script is attached to Sphere.
In this code, I guessed if Sphere ignore other object , AddForce to Sphere at the time they collide can be solution. But it doesn't work also.
function Update () {
var additional = 0.01;/
// If gameObject goes down, it surely collide with other Object with tag "Barrier"
gameObject.transform.position = Vector3(gameObject.transform.position.x, gameObject.transform.position.y - additional, gameObject.transform.position.z);
}
function OnCollisionEnter(collision : Collision) {
if (collision.collider.tag == "Barrier") {
rigidbody.AddForce(Vector3.up * 100);//This does't work also
}
}
I tried to solve this problem for two days, but I could't.
If you give me help, I'll be so glad...
Thanks.
Setting transform.position ignores collisions, thus you can't have both things at the same time. What do you want to do when colliding with other objects? Push them away or block the sphere movement?
Thanks for comment :)
Yeah, actually I want them to push the Sphere away..
Oh, It successfully worked, my Sphere bounce with other objects !!!
Thank you so much such for giving me such a excellent answer, Your example is very easy fo me to understand... And sorry to be a just a beginner but, " The engine uses velocity to move, bounce and so on." is a great tip for me.
Thanks you $$anonymous$$r.Owen and $$anonymous$$r.aldonaletto :)
I have the same problem in my game but i dont want to use a rigidbody. is there any way to use the transform.position and still collide with stuff?
Answer by Owen-Reynolds · Nov 20, 2011 at 05:58 PM
The physics engine handles collisions and such during a secret physics step that you don't program. To use it, make sure the object has a rigidbody (looks like yours does) and set rigidbody.velocity
. The engine uses velocity to move, bounce and so on.
If you move with tranform.Translate
or transform =
, it's like you are repositioning or teleporting the object. The next physics step says "hey, wasn't this speed 0 object over there last time? Now it's partially inside this goat. Oh, well."
A cheap way to set velocity, but also follow a curve is to "aim at" a spot just ahead of you on the curve. Something like:
Vector3 pos2 = next spot on the curve
// NOTE: p2-pos is direction to next spot from old spot
// normalized*speed is standard way to turn direction into constant speed
Vector3 spd = (pos2-pos).normalized * speed
rigidbody.velocity = spd;
It's a little like guiding a dog around a racetrack with a moving bunny (the dog is the object and the bunny is the "next position".) You won't guide the dog exactly where you want it, but close enough. You have to move the "next point" and the dog at the right speeds so the dog doesn't overshoot and turn around; and also slow enough so the dog doesn't cut across the track. So, say you use time to pick next spot. Just have to tweak the object's speed.
Just to complete this great answer: you should do this at FixedUpdate, and you could set the velocity this way:
rigidbody.velocity = (pos2-pos)/Time.deltaTime;
This sets the rigidbody.velocity exactly to the direction and speed necessary to reach the next point in the next physics cycle.
Your answer
Follow this Question
Related Questions
how to I zero out a parented child's position in X,Y,Z on collision? 1 Answer
Detect collision/trigger between two body without rigidbody? 3 Answers
Use GameObject's global position instead of local position 3 Answers
Need insight on strange (simple) 2D object transform bug 0 Answers
Why I've a gap between meshcolliders? 0 Answers