Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. We are looking for feedback on the naming of a new user research platform that we are working on.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

(URGENT) JumpPad Help.

Discussion in 'Scripting' started by MrBlub2010, Oct 15, 2020.

  1. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    I have a game that i want to make and i want to add a jumpPad but i cant figure out what to write in VS Code. I want my jumpPad to make me jump when i collide with it. Any tutorials or scripts i can use???

    Using Unity 2020
    Links: N/A
    CharacterController:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     //Assingables
    7.     public Transform playerCam;
    8.     public Transform orientation;
    9.    
    10.     //Other
    11.     private Rigidbody rb;
    12.  
    13.     //Rotation and look
    14.     private float xRotation;
    15.     private float sensitivity = 50f;
    16.     private float sensMultiplier = 1f;
    17.    
    18.     //Movement
    19.     public float moveSpeed = 4500;
    20.     public float maxSpeed = 20;
    21.     public bool grounded;
    22.     public LayerMask whatIsGround;
    23.    
    24.     public float counterMovement = 0.175f;
    25.     private float threshold = 0.01f;
    26.     public float maxSlopeAngle = 35f;
    27.  
    28.     //Crouch & Slide
    29.     private Vector3 crouchScale = new Vector3(1, 0.5f, 1);
    30.     private Vector3 playerScale;
    31.     public float slideForce = 400;
    32.     public float slideCounterMovement = 0.2f;
    33.  
    34.     //Jumping
    35.     private bool readyToJump = true;
    36.     private float jumpCooldown = 0.25f;
    37.     public float jumpForce = 100f;
    38.    
    39.     //Input
    40.     float x, y;
    41.     bool jumping, sprinting, crouching;
    42.    
    43.     //Sliding
    44.     private Vector3 normalVector = Vector3.up;
    45.     private Vector3 wallNormalVector;
    46.  
    47.     void Awake() {
    48.         rb = GetComponent<Rigidbody>();
    49.     }
    50.    
    51.     void Start() {
    52.         playerScale =  transform.localScale;
    53.         Cursor.lockState = CursorLockMode.Locked;
    54.         Cursor.visible = false;
    55.     }
    56.  
    57.    
    58.     private void FixedUpdate() {
    59.         Movement();
    60.     }
    61.  
    62.     private void Update() {
    63.         MyInput();
    64.         Look();
    65.     }
    66.  
    67.     /// <summary>
    68.     /// Find user input. Should put this in its own class but im lazy
    69.     /// </summary>
    70.     private void MyInput() {
    71.         x = Input.GetAxisRaw("Horizontal");
    72.         y = Input.GetAxisRaw("Vertical");
    73.         jumping = Input.GetButton("Jump");
    74.         crouching = Input.GetKey(KeyCode.LeftControl);
    75.      
    76.         //Crouching
    77.         if (Input.GetKeyDown(KeyCode.LeftControl))
    78.             StartCrouch();
    79.         if (Input.GetKeyUp(KeyCode.LeftControl))
    80.             StopCrouch();
    81.     }
    82.  
    83.     private void StartCrouch() {
    84.         transform.localScale = crouchScale;
    85.         transform.position = new Vector3(transform.position.x, transform.position.y - 0.5f, transform.position.z);
    86.         if (rb.velocity.magnitude > 0.5f) {
    87.             if (grounded) {
    88.                 rb.AddForce(orientation.transform.forward * slideForce);
    89.             }
    90.         }
    91.     }
    92.  
    93.     private void StopCrouch() {
    94.         transform.localScale = playerScale;
    95.         transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f, transform.position.z);
    96.     }
    97.  
    98.     private void Movement() {
    99.         //Extra gravity
    100.         rb.AddForce(Vector3.down * Time.deltaTime * 10);
    101.        
    102.         //Find actual velocity relative to where player is looking
    103.         Vector2 mag = FindVelRelativeToLook();
    104.         float xMag = mag.x, yMag = mag.y;
    105.  
    106.         //Counteract sliding and sloppy movement
    107.         CounterMovement(x, y, mag);
    108.        
    109.         //If holding jump && ready to jump, then jump
    110.         if (readyToJump && jumping) Jump();
    111.  
    112.         //Set max speed
    113.         float maxSpeed = this.maxSpeed;
    114.        
    115.         //If sliding down a ramp, add force down so player stays grounded and also builds speed
    116.         if (crouching && grounded && readyToJump) {
    117.             rb.AddForce(Vector3.down * Time.deltaTime * 3000);
    118.             return;
    119.         }
    120.        
    121.         //If speed is larger than maxspeed, cancel out the input so you don't go over max speed
    122.         if (x > 0 && xMag > maxSpeed) x = 0;
    123.         if (x < 0 && xMag < -maxSpeed) x = 0;
    124.         if (y > 0 && yMag > maxSpeed) y = 0;
    125.         if (y < 0 && yMag < -maxSpeed) y = 0;
    126.  
    127.         //Some multipliers
    128.         float multiplier = 1f, multiplierV = 1f;
    129.        
    130.         // Movement in air
    131.         if (!grounded) {
    132.             multiplier = 0.5f;
    133.             multiplierV = 0.5f;
    134.         }
    135.        
    136.         // Movement while sliding
    137.         if (grounded && crouching) multiplierV = 0f;
    138.  
    139.         //Apply forces to move player
    140.         rb.AddForce(orientation.transform.forward * y * moveSpeed * Time.deltaTime * multiplier * multiplierV);
    141.         rb.AddForce(orientation.transform.right * x * moveSpeed * Time.deltaTime * multiplier);
    142.     }
    143.  
    144.     private void Jump() {
    145.         if (grounded && readyToJump) {
    146.             readyToJump = false;
    147.  
    148.             //Add jump forces
    149.             rb.AddForce(Vector2.up * jumpForce * 1.5f);
    150.             rb.AddForce(normalVector * jumpForce * 0.5f);
    151.            
    152.             //If jumping while falling, reset y velocity.
    153.             Vector3 vel = rb.velocity;
    154.             if (rb.velocity.y < 0.5f)
    155.                 rb.velocity = new Vector3(vel.x, 0, vel.z);
    156.             else if (rb.velocity.y > 0)
    157.                 rb.velocity = new Vector3(vel.x, vel.y / 2, vel.z);
    158.            
    159.             Invoke(nameof(ResetJump), jumpCooldown);
    160.         }
    161.     }
    162.    
    163.     private void ResetJump() {
    164.         readyToJump = true;
    165.     }
    166.    
    167.     private float desiredX;
    168.     private void Look() {
    169.         float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
    170.         float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime * sensMultiplier;
    171.  
    172.         //Find current look rotation
    173.         Vector3 rot = playerCam.transform.localRotation.eulerAngles;
    174.         desiredX = rot.y + mouseX;
    175.        
    176.         //Rotate, and also make sure we dont over- or under-rotate.
    177.         xRotation -= mouseY;
    178.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    179.  
    180.         //Perform the rotations
    181.         playerCam.transform.localRotation = Quaternion.Euler(xRotation, desiredX, 0);
    182.         orientation.transform.localRotation = Quaternion.Euler(0, desiredX, 0);
    183.     }
    184.  
    185.     private void CounterMovement(float x, float y, Vector2 mag) {
    186.         if (!grounded || jumping) return;
    187.  
    188.         //Slow down sliding
    189.         if (crouching) {
    190.             rb.AddForce(moveSpeed * Time.deltaTime * -rb.velocity.normalized * slideCounterMovement);
    191.             return;
    192.         }
    193.  
    194.         //Counter movement
    195.         if (Math.Abs(mag.x) > threshold && Math.Abs(x) < 0.05f || (mag.x < -threshold && x > 0) || (mag.x > threshold && x < 0)) {
    196.             rb.AddForce(moveSpeed * orientation.transform.right * Time.deltaTime * -mag.x * counterMovement);
    197.         }
    198.         if (Math.Abs(mag.y) > threshold && Math.Abs(y) < 0.05f || (mag.y < -threshold && y > 0) || (mag.y > threshold && y < 0)) {
    199.             rb.AddForce(moveSpeed * orientation.transform.forward * Time.deltaTime * -mag.y * counterMovement);
    200.         }
    201.        
    202.         //Limit diagonal running. This will also cause a full stop if sliding fast and un-crouching, so not optimal.
    203.         if (Mathf.Sqrt((Mathf.Pow(rb.velocity.x, 2) + Mathf.Pow(rb.velocity.z, 2))) > maxSpeed) {
    204.             float fallspeed = rb.velocity.y;
    205.             Vector3 n = rb.velocity.normalized * maxSpeed;
    206.             rb.velocity = new Vector3(n.x, fallspeed, n.z);
    207.         }
    208.     }
    209.  
    210.     /// <summary>
    211.     /// Find the velocity relative to where the player is looking
    212.     /// Useful for vectors calculations regarding movement and limiting movement
    213.     /// </summary>
    214.     /// <returns></returns>
    215.     public Vector2 FindVelRelativeToLook() {
    216.         float lookAngle = orientation.transform.eulerAngles.y;
    217.         float moveAngle = Mathf.Atan2(rb.velocity.x, rb.velocity.z) * Mathf.Rad2Deg;
    218.  
    219.         float u = Mathf.DeltaAngle(lookAngle, moveAngle);
    220.         float v = 90 - u;
    221.  
    222.         float magnitue = rb.velocity.magnitude;
    223.         float yMag = magnitue * Mathf.Cos(u * Mathf.Deg2Rad);
    224.         float xMag = magnitue * Mathf.Cos(v * Mathf.Deg2Rad);
    225.        
    226.         return new Vector2(xMag, yMag);
    227.     }
    228.  
    229.     private bool IsFloor(Vector3 v) {
    230.         float angle = Vector3.Angle(Vector3.up, v);
    231.         return angle < maxSlopeAngle;
    232.     }
    233.  
    234.     private bool cancellingGrounded;
    235.    
    236.     /// <summary>
    237.     /// Handle ground detection
    238.     /// </summary>
    239.     private void OnCollisionStay(Collision other) {
    240.         //Make sure we are only checking for walkable layers
    241.         int layer = other.gameObject.layer;
    242.         if (whatIsGround != (whatIsGround | (1 << layer))) return;
    243.  
    244.         //Iterate through every collision in a physics update
    245.         for (int i = 0; i < other.contactCount; i++) {
    246.             Vector3 normal = other.contacts[i].normal;
    247.             //FLOOR
    248.             if (IsFloor(normal)) {
    249.                 grounded = true;
    250.                 cancellingGrounded = false;
    251.                 normalVector = normal;
    252.                 CancelInvoke(nameof(StopGrounded));
    253.             }
    254.         }
    255.  
    256.         //Invoke ground/wall cancel, since we can't check normals with CollisionExit
    257.         float delay = 3f;
    258.         if (!cancellingGrounded) {
    259.             cancellingGrounded = true;
    260.             Invoke(nameof(StopGrounded), Time.deltaTime * delay);
    261.         }
    262.     }
    263.  
    264.     private void StopGrounded() {
    265.         grounded = false;
    266.     }
    267.    
    268. }
    269.  
    270.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    4,270
    It looks like you're using Rigidbody motion for your character. The simple answer would be to add a force to your player in OnTriggerEnter using
    ForceMode.VelocityChange
    . Naturally you will need a trigger collider on the jump pad for this too.
     
  3. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    I still dont understand... :(
     
  4. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
  5. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    If i tick the Is Trigger button it makes me fall strate through.
     
  6. MrBlub2010

    MrBlub2010

    Joined:
    Aug 24, 2020
    Posts:
    66
    anyone?
     
unityunity