- Home /
ThirdPersonCharacter script blocks trampoline jump
(I'm brazillian, sorry for the english) My trampoline script:
using UnityEngine; using System.Collections;
public class Trampoline : MonoBehaviour {
Animator anim;
[System.NonSerialized]public bool onTop;
GameObject bouncer;
public Vector3 JumpPower;
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
}
void OnCollisionStay(Collision other)
{
if(onTop)
{
anim.SetBool("isStepped", true);
bouncer = other.gameObject;
}
}
void OnTriggerEnter()
{
onTop = true;
}
void OnTriggerExit()
{
onTop = false;
anim.SetBool("isStepped", false);
}
void Jump()
{
bouncer.GetComponent<Rigidbody>().velocity = JumpPower;
}
}
others objects jump fine, and if I disable just one of the two ThirdPersonCharacter scripts of my character work too, I try many things but I can't make it work, if you want to see the a little bit of ThirdPersonCharacter script, I think this is caused by velocity var(Vector3) and the manipulation of useGravity Comand, thank you for the patience:
void Update()
{
if(onGround)
{
doubleJumped = false;
}
public void Move(float h, float v, bool crouch, bool jump, Vector3 lookPos, Vector3 move)
{
if (h != 0f || v != 0f)
{
rotate(v,h);
animator.SetFloat ("Forward", 1f, 0.1f, Time.deltaTime);
}
else {
animator.SetFloat ("Forward", 0f, 0.1f, Time.deltaTime);
}
//if (move.magnitude > 1) move.Normalize();
// transfer input parameters to member variables.
this.moveInput = move;
this.crouchInput = crouch;
this.jumpInput = jump;
this.currentLookPos = lookPos;
// grab current velocity, we will be changing it.
velocity = GetComponent<Rigidbody>().velocity;
ConvertMoveInput(); // converts the relative move vector into local turn & fwd values
// TurnTowardsCameraForward(); // makes the character face the way the camera is looking
PreventStandingInLowHeadroom(); // so the character's head doesn't penetrate a low ceiling
ScaleCapsuleForCrouching(); // so you can fit under low areas when crouching
// ApplyExtraTurnRotation(); // this is in addition to root rotation in the animations
GroundCheck(); // detect and stick to ground
SetFriction(); // use low or high friction values depending on the current state
// control and velocity handling is different when grounded and airborne:
if (onGround)
{
HandleGroundedVelocities();
}
else
{
HandleAirborneVelocities();
}
UpdateAnimator(); // send input and other state parameters to the animator
// reassign velocity, since it will have been modified by the above functions.
GetComponent<Rigidbody>().velocity = velocity;
}
private void GroundCheck()
{
Ray ray = new Ray(transform.position + Vector3.up*.1f, -Vector3.up);
RaycastHit[] hits = Physics.RaycastAll(ray, .5f,groundCheckMask);
System.Array.Sort(hits, rayHitComparer);
if (velocity.y < jumpPower*.5f)
{
onGround = false;
GetComponent<Rigidbody>().useGravity = true;
foreach (var hit in hits)
{
// check whether we hit a non-trigger collider (and not the character itself)
if (!hit.collider.isTrigger)
{
// this counts as being on ground.
// stick to surface - helps character stick to ground - specially when running down slopes
if (velocity.y <= 0)
{
GetComponent<Rigidbody>().position = Vector3.MoveTowards(GetComponent<Rigidbody>().position, hit.point,
Time.deltaTime*advancedSettings.groundStickyEffect);
}
onGround = true;
GetComponent<Rigidbody>().useGravity = false;
break;
}
}
}
// remember when we were last in air, for jump delay
if (!onGround) lastAirTime = Time.time;
}
private void HandleGroundedVelocities()
{
velocity.y = 0;
if (moveInput.magnitude == 0)
{
// when not moving this prevents sliding on slopes:
velocity.x = 0;
velocity.z = 0;
}
// check whether conditions are right to allow a jump:
bool animationGrounded = animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded");
bool okToRepeatJump = Time.time > lastAirTime + advancedSettings.jumpRepeatDelayTime;
if (jumpInput && !crouchInput && /*okToRepeatJump &&*/ animationGrounded)
{
// jump!
onGround = false;
velocity = moveInput*airSpeed;
velocity.y = jumpPower;
jumpParticle.Emit(3);
}
}
private void HandleAirborneVelocities()
{
// we allow some movement in air, but it's very different to when on ground
// (typically allowing a small change in trajectory)
Vector3 airMove = new Vector3(moveInput.x*airSpeed, velocity.y, moveInput.z*airSpeed);
velocity = Vector3.Lerp(velocity, airMove, Time.deltaTime*airControl);
GetComponent<Rigidbody>().useGravity = true;
// apply extra gravity from multiplier:
Vector3 extraGravityForce = (Physics.gravity*gravityMultiplier) - Physics.gravity;
GetComponent<Rigidbody>().AddForce(extraGravityForce);
if(jumpInput && !doubleJumped)
{
velocity.y = jumpPower;
doubleJumped = true;
animator.SetTrigger("DoubleJump");
jumpParticle.Emit(3);
}
}
Comment
Your answer
