I am creating a faux gravity game in Unity. I tried it with a rigid body character controller script posted here:
var speed = 10.0;
var gravity = 10.0;
var maxVelocityChange = 10.0;
var canJump = true;
var jumpHeight = 2.0;
private var grounded = false;
@script RequireComponent(Rigidbody, CapsuleCollider)
function Awake ()
{
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
}
function FixedUpdate ()
{
if (grounded)
{
// Calculate how fast we should be moving
var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0.1, Input.GetAxis("Vertical"));
//var targetVelocity = new Vector3(Input.GetAxis("Vertical"), 0.1, Input.GetAxis("Horizontal"));
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= speed;
// Apply a force that attempts to reach our target velocity
var velocity = rigidbody.velocity;
var velocityChange = (targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0.1;
rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
// Jump
if (canJump Input.GetButton("Jump"))
{
rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
}
}
// We apply gravity manually for more tuning control
rigidbody.AddForce(Vector3 (0.1, -gravity * rigidbody.mass, 0.1));
grounded = false;
}
function OnCollisionStay ()
{
grounded = true;
}
function CalculateJumpVerticalSpeed ()
{
// From the jump height and gravity we deduce the upwards speed
// for the character to reach at the apex.
return Mathf.Sqrt(2 * jumpHeight * gravity);
}
and it worked fine!
then when i was trying to use the 3rd person controller script in the library (the construction guy script) with the faux gravity, it didn’t work. It made the character go through the world and jump out of the other side. It flew without stopping. I created the character with Maya 2011. I want to use the character with faux gravity and with my 4 animation clips (walking, running, idle, and jump_pose,) along with all of the faux gravity scripts. I was wondering if there is a specific script that would allow me to use my third person controller with the faux gravity and all of my movement animations at the same time.
Here is the faux gravity body script that I used:
/*
FauxGravityBody.js
Written by Tonio Loewald ©2008
Attach this script to objects you want to be affected by FauxGravity
*/
// this is the thing we're gravitationally attracted to
var attractor : FauxGravityAttractor;
// are we touching the surface?
var grounded : int;
function Start () {
rigidbody.WakeUp();
rigidbody.useGravity = false;
}
// obviously this is crude since we might want to be able to stand on (and jump off) random objects
// should probably filter based on tag in future
function OnCollisionEnter (c : Collision) {
if( c.gameObject.layer == 10 ){
grounded ++;
}
}
function OnCollisionExit (c : Collision) {
if( c.gameObject.layer == 10 grounded > 0 ){
grounded --;
}
}
function FixedUpdate () {
if(attractor){
attractor.Attract(this);
}
}
@script RequireComponent(Rigidbody)
and then the attractor script:
/*
FauxGravityAttractor.js
Written by Tonio Loewald ©2008
Attach this script to objects you want to exert Faux Gravity
*/
// Set to true for mono-directional gravity
var useLocalUpVector : boolean = false;
// Force applied along gravity up-vector (negative = down)
var fauxGravity = -10.0;
function Attract ( body : FauxGravityBody ){
var gravityUp : Vector3;
var localUp: Vector3;
var localForward : Vector3;
var t : Transform = body.transform;
var r : Rigidbody = body.rigidbody;
// Figure out the body's up vector
if(useLocalUpVector){
gravityUp = transform.up;
} else {
gravityUp = t.position - transform.position;
gravityUp.Normalize();
}
// Accelerate the body along its up vector
r.AddForce( gravityUp * fauxGravity * r.mass );
r.drag = body.grounded ? 1 : 0.1;
// If the object's freezerotation is set, we force the object upright
if(r.freezeRotation){
// Orient relatived to gravity
localUp = t.up;
var q = Quaternion.FromToRotation(localUp, gravityUp);
q = q * t.rotation;
t.rotation = Quaternion.Slerp(t.rotation, q, 0.1);
localForward = t.forward;
}
}
I need an answer ASAP please!!
Thanks,
-Miriam