- Home /
isKinematic object doesnt collide with wall
Hey i need help for my little game project.
I have a ball (has a collider and kinematic rigidbody) and i m moving that with "rb.MovePosition" method. But that can pass through walls. I dont want it. Walls are simple cubes and my ball should stop when collide a wall. Can you pls help me ?
Answer by exp626stitch · Dec 16, 2020 at 06:39 PM
@sefayilmaz A raycast:
Vector3 lastPos;
public void Start() {
lastPos = transform.position;
}
public void FixedUpdate()
{
lastPos = transform.position;
RaycastHit[] hits = Physics.RaycastAll(new Ray(lastPos, (transform.position - lastPos).normalized), (transform.position - lastPos).magnitude);
//Do something with it using hit
transform.position = hit[1].point;
}
If a collision dosnt occur in a visible frame, it didnt happen, so the raycast goes between where you were and where you are, and if there is a collision, then it teleports the object to the collision point, although I would recommend adding a offset based on half your size.
I don't know exactly how to use the raycast system. Here my moving script code. How to implement the raycasting system to this ? Sorry for noob question...` void Update() { if (Input.GetKey(KeyCode.RightArrow)) { rb.$$anonymous$$ovePosition(rb.position + transform.right 50 Time.deltaTime); } }`
Answer by sefayilmaz · Dec 16, 2020 at 07:38 PM
I don't know exactly how to use the raycast system. Here my moving script code. How to implement the raycasting system to this ? Sorry for noob question...
void Update()
{
if (Input.GetKey(KeyCode.RightArrow))
{
rb.MovePosition(rb.position + transform.right * 50 * Time.deltaTime);
}
}
Answer by rokushimizu1022ghibli · Dec 17, 2020 at 01:51 AM
If the object is kinematic, it means that no physics' method applies to the object: no gravity, no force, and no collision. Did you search what "isKinematic" boolean means?
If isKinematic is enabled, Forces, collisions, or joints will not affect the rigidbody anymore. https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html
If you don't really want it to be kinematic, just disable it.
I know what that means but i just need isKinematic object. I m using $$anonymous$$ovePosition method for it. But there should be a way to dedect collisions. I think raycast system works but i do not know how to implement it to my code.
Then, write a code as Exp626stitch has showed. If you don't know, see the documentation. [https://docs.unity3d.com/2019.4/Documentation/ScriptReference/Physics.Raycast.html][1]