- Home /
Javascript: Character won't stay grounded.
I started following an video series of tutorials for scripting an fps. I started adding mecanim animations including a jump pose. Unfortunately, "grounded" is only set to "true" immediately as my character hits the floor, and becomes stuck in "false".
I tried incorporating different types of JavaScript to resolve the issue but I so far have no success.
Current script:
var walkAcceleration : float = 500;
var walkAccelAirRatio : float = 0.1;
var walkDeacceleration : float = 0.5;
@HideInInspector
var walkDeaccelerationVolx : float;
@HideInInspector
var walkDeaccelerationVolz : float;
var cameraObject : GameObject;
var maxWalkSpeed : float = 50;
@HideInInspector
var horizontalMovement : Vector2;
var jumpVelocity : float = 250;
@HideInInspector
var grounded : boolean = false;
var maxSlope : float = 60;
@HideInInspector
var hor : float;
@HideInInspector
var ver : float;
@HideInInspector
var collisionObject : GameObject;
internal var animator : Animator;
function Start ()
{
animator = GetComponent(Animator);
}
function Update ()
{
hor = Input.GetAxis("Horizontal");
ver = Input.GetAxis("Vertical");
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if (horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement = horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;
if (grounded)
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);
transform.rotation = Quaternion.Euler(0, cameraObject.GetComponent(MouseLook).currentYRotation, 0);
if (grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkAcceleration * walkAccelAirRatio * Time.deltaTime, 0, Input.GetAxis("Vertical") * walkAcceleration * walkAccelAirRatio * Time.deltaTime);
if (Input.GetButtonDown("Jump") && grounded)
rigidbody.AddForce(0, jumpVelocity, 0);
}
function FixedUpdate ()
{
animator.SetFloat("Walking", ver);
}
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
function OnCollisionExit ()
{
grounded = false;
}
Answer by CyberArtist · Jan 01, 2015 at 01:45 AM
I took this question to the forums and I managed to figure it out: http://forum.unity3d.com/threads/function-oncollisionstay-not-being-called-every-frame.288497/
Answer by ks9109 · Dec 31, 2014 at 05:47 PM
try making it if else statement rather than just if
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
function OnCollisionStay (collision : Collision)
{
for (var contact : ContactPoint in collision.contacts)
{
if else (Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
grounded = true;
}
}
I have an error. I don't know how to do it.
2nd: the collision is only detected once. How do I make the collision remain constant while I still make contact to the ground?
Your answer
![](https://koobas.hobune.stream/wayback/20220613175307im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can Mecanim/Animator be used to make Enemy AI's? -1 Answers
rotate Y axis to face mouse 1 Answer
User Created Animations Not Working With Mecanim 0 Answers
How to get the values of the animation? 0 Answers
Javascript controlling Mecanim? 1 Answer