- Home /
How do I write a script so that an object rotates and follows the contours of a larger object?
First, I am newer to scripting than most of the people here that also make such claims. So basically, I would like to be able to walk around the inside circumference of a cylinder with an FPS controller. Is it possible to write a script that keeps the moving object "grounded" (perpendicular) to the geometry of the cylinder? Another example of what I'm looking for could be applied to: driving a car through a vertical loop.
I have no idea where to even begin scripting anything like this.
I feel your pain. I actually want basically the same thing: http://answers.unity3d.com/questions/3378/physics-objects-in-a-curved-2d-plane Rather than a loop it's even simpler in a way. It's "just" a curve. Just want to tell you I think there is no simple answer for this one. :(
Answer by Stelimar · Nov 29, 2009 at 07:29 AM
From the collision object passed to the OnCollisionStay event, you can access a list of contacts, and get their normals. However, there could potentially be multiple contacts, which means you'll have more than one normal. To deal with this, I would simply add the normals together, and normalize, which would give you something like this:
function OnCollisionStay(col : Collision) {
var colNormal : Vector3 = new Vector3();
for (var contact : ContactPoint in col.contacts) {
colNormal += contact.normal;
}
colNormal.Normalize();
transform.up = colNormal;
}
Thank you for your help! But, I'm not exactly sure what I am supposed to do. I attached the script to the cylinder... then how do I access the list of contacts and link that with the FPS controller?
I am also getting a few errors from the code:
(Filename: Assets/colnorm.js Line: 1)
Assets/colnorm.js(1,23): BCE0043: Unexpected token: Collision.
(Filename: Assets/colnorm.js Line: 1)
Assets/colnorm.js(2,5): BCE0043: Unexpected token: var.
(Filename: Assets/colnorm.js Line: 2)
Assets/colnorm.js(2,8): UCE0001: ';' expected. Insert a semicolon at the end.
(Filename: Assets/colnorm.js Line: 2)
Assets/colnorm.js(2,9): BCE0044: expecting EOF, found 'colNormal'.
@Stelimar: Well, he said he knew absolutely nothing about script. This answer was really not for you, @Adamsw216. It's quite technical. :P
By the way, that script is just a guideline and a part of what would go on the FPS Controller object - not the cylinder.