- Home /
how to make Vector3.MoveTowards accept physics
hi, i need to make my G.O change position between 2 vectors but the problem is that he ignoring physics, i want him to stop if he collided with any other object, any ideas? thnx :)
Answer by yeoldesnake 1 · Oct 20, 2011 at 04:11 PM
Add this after the if (Physics.Raycast(ray, out hit, Mathf.Infinity, myLayerMask)) check.
if (Input.GetMouseButtonDown(0)){
if (Physics.Linecast (transform.position, target.position,hitInfo)){
//movement code here , tell it to move to hitInfo.point
}
else
//tell it to move to target.position
}
this doesn't make sense .... if he can't move while he is colliding how can he go away.
He is not colliding all the time. He wants it to move unless it meets something in the way. Sometimes it will sometimes it wont , it depends on how he intends to use the movement. EDIT: So i see that the movement is for a character? I thought you were trying to move an object , i am going to update the answer with a solution that uses raycasting.
i'm Already Using Raycast :
Ray ray = myCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, $$anonymous$$athf.Infinity, myLayer$$anonymous$$ask))
{
if (Input.Get$$anonymous$$ouseButtonDown(0))
nextPos.position = hit.point;
}
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, nextPos.position, speed * Time.deltaTime);
but i need to make him collide
Take a look at my solution , i am using linecast from the character's position to the destination's position to see if there is anything in between. If there is , it will move to where the linecast hit the object , which is right behind the wall.
Answer by TSorbera · Oct 20, 2011 at 05:34 PM
If you want to use physics, you should probably be moving the character with physics. Try Rigidbody.AddForce
or AddRelativeForce
.
That too , i was operating under the thought that he wanted it to not have a rigidbody.
this wont work for me cus i'm moving the object from point A to point B
http://unity3d.com/support/documentation/ScriptReference/Physics.CapsuleCast.html See if that will work for you. If not, another option is to add a collider to your object if there's not one already, and use OnCollision/TriggerEnter/Stay/Exit: http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.OnCollisionEnter.html
Your answer
Follow this Question
Related Questions
2D 360 degress platformer example needed 0 Answers
Why isn't my script to move particles working? 0 Answers
How to make physics like Angry Birds Space -1 Answers
2d side scrolling problem 5 Answers
Move RigidBody along curve 2 Answers