# Changing gravity on collision

I’m making a game where players can walk on walls and I have this code allowing me to change gravity

var walkSpeed = 8.0;
var walkBackwardSpeed = 4.0;
var sidestepSpeed = 8.0;
var gravity = 10.0;
var maxVelocityChange = 10.0;
var inAirControl = 0.1;
var canJump = true;
var jumpHeight = 2.0;

// added for run (set to Fire2. Change in Project Settings / Input if different desired.)
var canRun = true;
var runSpeed = 14.0; // negative values here makes game unhappy
var runBackwardSpeed = 6.0; // negative values here makes game unhappy
// var runSpeedChange = 4.0; // negative values here makes game unhappy

var canRunSidestep = true;
var runSidestepSpeed = 12.0;

// added for keyboard rotation (‘Horizontal’ rotates instead of translates / strafe)
/*var canRotate = false;
var rotateSpeed = 1.0;
var rotateInAir = false;
*/
private var grounded = false;
private var groundVelocity : Vector3;
private var capsule : CapsuleCollider;
private var gravityDirection = Vector3(0,-1,0);

@script RequireComponent(Rigidbody, CapsuleCollider)

function Awake ()
{
rigidbody.freezeRotation = true;
rigidbody.useGravity = false;
capsule = GetComponent(CapsuleCollider);
}

function FixedUpdate ()
{
if (grounded)
{
// Calculate how fast we should rotate
// var rotation = Input.GetAxis(“Horizontal”) * rotateSpeed;

``````    // Set 'Horizontal' input based on rotate variable
/*var targetVelocity = (canRotate) ? new Vector3(0, rotation, Input.GetAxis("Vertical"))
: new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));*/

// Calculate how fast we should be moving

targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

targetVelocity = transform.TransformDirection(targetVelocity);
if (Input.GetAxis("Vertical") > 0)
{
targetVelocity.x *= (canRun && Input.GetButton("Fire2")) ? runSpeed : walkSpeed;
}
else
{
targetVelocity.x *= (canRun && Input.GetButton("Fire2")) ? runBackwardSpeed : walkBackwardSpeed;
}

targetVelocity.z *= (canRunSidestep && Input.GetButton("Fire2")) ? runSidestepSpeed : sidestepSpeed;

// Rotate if rotate is enabled
/*if (canRotate)
{
transform.Rotate(0, rotation, 0);
}
*/

// Apply a force that attempts to reach our target velocity
var velocity = rigidbody.velocity;
var velocityChange = (targetVelocity - velocity) + groundVelocity;
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;

// Jump
if (canJump && Input.GetButton("Jump"))
{
//rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);

rigidbody.velocity = -gravityDirection * CalculateJumpVerticalSpeed();

}

grounded = false;
}

else
{

// Calculate how fast we should rotate
/*rotation = Input.GetAxis("Horizontal") * rotateSpeed;

// Set 'Horizontal' input behavior based on whether we can rotate in air
targetVelocity = (rotateInAir) ? new Vector3(0, rotation, Input.GetAxis("Vertical"))
: new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
*/
targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

targetVelocity = transform.TransformDirection(targetVelocity) * inAirControl;

// Rotate if rotate in air is enabled.
/*if (rotateInAir)
{
transform.Rotate(0, rotation, 0);
}
*/
}

``````

}

function TrackGrounded (col : Collision)
{
var minimumHeight = capsule.bounds.min.y + capsule.radius;
for (var c : ContactPoint in col.contacts)
{
if (c.point.y < minimumHeight)
{
//we track velocity for rigidbodies we’re standing on
if (col.rigidbody) groundVelocity = col.rigidbody.velocity;
//and become children of non-rigidbody colliders
else transform.parent = col.transform;
grounded = true;

``````if (col.tag == "XZdown")
gravityDirection = Vector3(0,-1,0);
else if (col.tag == "XYback")
gravityDirection = Vector3(0,0,-1);
else if (col.tag == "YZleft")
gravityDirection = Vector3(-1,0,0);
else if (col.tag == "XZup")
gravityDirection = Vector3(0,1,0);

}
``````

}
}

//unparent if we are no longer standing on our parent
function OnCollisionExit (col : Collision)
{
if (col.transform == transform.parent) transform.parent = null;
}

function OnCollisionStay (col : Collision)
{
TrackGrounded (col);
}

function OnCollisionEnter (col : Collision)
{
TrackGrounded (col);

}

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);
}

I copied the code from 125here125 and tweaked it a bit but I can’t figure out how to make it so that when a player collides with an object with the tag XZup, for example, to make the gravity pull that character upwards.

I haven’t read a line of your code. Let’s be honest, nobody will.

But I can still answer though. First you need to detect when the collision occurs (function OnCollisionEnter(Collision collisionInfo)), then you need to check the tag (if( collisionInfo.transform.tag == “XZup”)) and finally you need to change the gravity. For the scene gravity, over here. For a single object gravity, over there.

Good luck.

function OnCollisionEnter (col : Collision)
{
TrackGrounded (col);

``````if (col.tag == "XZdown")
gravityDirection = Vector3(0,-1,0);
else if (col.tag == "XYback")
gravityDirection = Vector3(0,0,-1);
else if (col.tag == "YZleft")
gravityDirection = Vector3(-1,0,0);
else if (col.tag == "XZup")
gravityDirection = Vector3(0,1,0);
``````

}

This is what I have for that section but it didn’t work. It’s supposed to be single object gravity.

YES! IT WORKED! Thank you!

The player’s gravity can change now but I can’t get the player to rotate with the gravity. For example if the player begins to fall up, the ground will still be down and the ceiling will still be up however what was originally the ground should instead be up. How would I do this?