- Home /
Move along surface normal of hollowed pipe with cube
Hi all,
I love @Marsupilami 's script here in another thread - https://answers.unity.com/questions/545844/make-player-character-stick-to-the-level-mesh-and.html - it's allowing me to starting moving my player cube around a pipe wall much of the time!
One drawback for me though - I think part of my problem is that I have to allow all sides of the cube to be the normal to the surface. Here's my movement code:
// Update is called once per frame use fixed update for Unity Fizzix
void FixedUpdate () {
//distanceFromPipeCenter.Normalize();
//add forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey(KeyCode.RightArrow) /*&& !fauxGravity*/)
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey(KeyCode.LeftArrow) /*&& !fauxGravity*/)
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
//if (Input.GetKey(KeyCode.RightArrow) && fauxGravity)
//{
// //Debug.Log("Right pressed");
// rb.AddForce(sidewaysForce * pipePull.x*Time.deltaTime, sidewaysForce * pipePull.y * Time.deltaTime, 0, ForceMode.VelocityChange);
// Debug.Log(rb.angularVelocity);
// float headingDeltaAngle = Input.GetAxis("Horizontal") * Time.deltaTime * sidewaysForce;
// Quaternion headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up);
// //align with surface normal
// transform.rotation = Quaternion.FromToRotation(transform.up, distanceFromPipeCenter) * transform.rotation;
// //apply heading rotation
// transform.rotation = headingDelta * transform.rotation;
//}
//if (Input.GetKey(KeyCode.LeftArrow) && fauxGravity)
//{
// //Debug.Log("Left pressed");
// rb.AddForce(-sidewaysForce * pipePull.x * Time.deltaTime, -sidewaysForce * pipePull.y * Time.deltaTime, 0, ForceMode.VelocityChange);
// Debug.Log(rb.angularVelocity);
// float headingDeltaAngle = Input.GetAxis("Horizontal") * Time.deltaTime * sidewaysForce;
// Quaternion headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up);
// //align with surface normal
// transform.rotation = Quaternion.FromToRotation(transform.up, distanceFromPipeCenter) * transform.rotation;
// //apply heading rotation
// transform.rotation = headingDelta * transform.rotation;
//}
if (fauxGravity)
{
float distForward = Mathf.Infinity;
RaycastHit hitForward;
if (Physics.SphereCast(transform.position, 0.25f, -transform.up + transform.forward, out hitForward, 5))
{
distForward = hitForward.distance;
}
float distDown = Mathf.Infinity;
RaycastHit hitDown;
if (Physics.SphereCast(transform.position, 0.25f, -transform.up, out hitDown, 5))
{
distDown = hitDown.distance;
}
float distBack = Mathf.Infinity;
RaycastHit hitBack;
if (Physics.SphereCast(transform.position, 0.25f, -transform.up + -transform.forward, out hitBack, 5))
{
distBack = hitBack.distance;
}
if (distForward < distDown && distForward < distBack)
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitForward.normal), hitForward.normal), Time.deltaTime * 5.0f);
}
else if (distDown < distForward && distDown < distBack)
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitDown.normal), hitDown.normal), Time.deltaTime * 5.0f);
}
else if (distBack < distForward && distBack < distDown)
{
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Cross(transform.right, hitBack.normal), hitBack.normal), Time.deltaTime * 5.0f);
}
GetComponent<Rigidbody>().AddForce(-transform.up * Time.deltaTime * 10);
}
if (rb.position.y <-1f)
{
FindObjectOfType<GameManagement>().EndGame();
}
}
You can see in my extra block of commented code, I've tried using an alternate method to detect when the player has collided with a hollowed pipe object (it starts moving inside the object). I want the cube to be attracted to the wall with a gravitational force relative to the surface of the pipe.
Any ideas?
Your answer
Follow this Question
Related Questions
Simulating magnetic shoes? 0 Answers
Object goes through wall. 0 Answers
Drawing a cube by a new Mesh 2 Answers
Normal Walker: Player Y Rotation Snapping back to 0? 0 Answers
Moving the cube on a surface. 1 Answer