- Home /
Question by
Blak30s · Feb 22, 2020 at 02:36 AM ·
raycast3dcolliderscollision detectionmovement script
How to restrict movement towards a collider.
I am creating a platformer like game and have been set the task to create my own script for movement and collisions rather than using the normal method of adding force to a rigid body and all my attempts haven't worked. Subject is the gameobject I will be moving, Limits is the collider assigned to the gameobject. This is my latest attempt but this one doesn't work either, please help.
public void moveSubject(float Tx, float Ty, float Tz )
{
Tx *= MaxMoveSpeed;
Ty *= MaxMoveSpeed;
RaycastHit hit;
if ((Physics.Raycast(Subject.transform.position,Vector3.right, out hit, (Limits.bounds.extents.x + 0.1f), 10)) && (Tx > 0))
{
Tx = 0;
}
if ((Physics.Raycast(Subject.transform.position, Vector3.left, out hit, (Limits.bounds.extents.x + 0.1f), 10)) && (Tx < 0))
{
Tx = 0;
}
if ((Physics.Raycast(Subject.transform.position, Vector3.up, out hit, (Limits.bounds.extents.y + 0.1f), 10)) && (Ty > 0))
{
Ty = 0;
}
if ((Physics.Raycast(Subject.transform.position, Vector3.down, out hit, (Limits.bounds.extents.y + 0.1f), 10)) && (Ty < 0))
{
Ty = 0;
}
TVector = new Vector3(Tx, Ty, Tz);
Subject.transform.Translate(TVector);
}
Comment
Answer by JoltenDev · Feb 24, 2020 at 07:03 AM
search up Brackey's video on 3rd person fps movement, he provides you with a movement script that is really easy to understand and add-on to. It doesn't use rigidbodies btw :)
Thank you ill check that out, i was worried it was a lost cause.