Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How can I stop the player at specific position after jump ?

Discussion in 'Scripting' started by Akashdj, Jul 28, 2020.

  1. Akashdj

    Akashdj

    Joined:
    Nov 5, 2017
    Posts:
    3
    Hi,
    This is an endless runner script. I am using character controller. My player default "y axis" position is 3 (He will be floating without any support). But after Jumping he will go below the "y axis" position 3 .

    I want the player "y axis" position to be in position 3 after jumping.

    Someone please help me with this script. How to stop the player "y axis" position at 3 after jumping.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class myplayer : MonoBehaviour
    7. {
    8.  
    9.     private const float LaneDistance = 2f;
    10.     private CharacterController controller;
    11.     private float jumpforce = 7f;
    12.     private float speed = 18f;
    13.     private float verticalvelocity = 0f;
    14.     public float gravity = 12f;
    15.     public int desiredLane = 1;
    16.     public int verticalLane = 1;
    17.     private Animator anim;
    18.     bool jumped;
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         controller = GetComponent<CharacterController>();
    23.         anim = GetComponent<Animator>();
    24.         jumped = false;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.  
    31.  
    32.         if (MobileInput.Instance.SwipeLeft)
    33.         {
    34.             MoveLane(false);
    35.             anim.SetTrigger("moveleft");
    36.  
    37.         }
    38.         if (MobileInput.Instance.SwipeRight)
    39.         {
    40.             MoveLane(true);
    41.             anim.SetTrigger("moveright");
    42.  
    43.  
    44.         }
    45.  
    46.         Vector3 targetPosition = transform.position.z * Vector3.forward;
    47.  
    48.         if (desiredLane == 0)
    49.         {
    50.             targetPosition += Vector3.left * LaneDistance;
    51.             print("des0");
    52.         }
    53.         else if (desiredLane == 2)
    54.         {
    55.             targetPosition += Vector3.right * LaneDistance;
    56.             print("des2");
    57.         }
    58.  
    59.  
    60.         if (transform.position.y == 3)
    61.         {
    62.             verticalvelocity = 0f;
    63.             if (MobileInput.Instance.SwipeUp)
    64.             {
    65.                 //jump code
    66.                 jumped = true;
    67.                 verticalvelocity = jumpforce;
    68.             }
    69.  
    70.         }
    71.         else
    72.         {
    73.             //I tried to stop the player at position 3. But he stops at 2.9 or 2.5 some values below 3
    74.             if (transform.position.y > 3 && jumped)
    75.             {
    76.  
    77.                 verticalvelocity -= (gravity * Time.deltaTime);
    78.             }
    79.             else
    80.             {
    81.                 verticalvelocity = 0;
    82.             }
    83.            
    84.         }
    85.  
    86.  
    87.         Vector3 movevector = Vector3.zero;
    88.         movevector.x = (targetPosition.x - transform.position.x) * speed;
    89.         movevector.y = verticalvelocity;
    90.         movevector.z = speed;
    91.         controller.Move(movevector * Time.deltaTime);
    92.  
    93.     }
    94.  
    95.  
    96.     private void MoveLane(bool goright)
    97.     {
    98.  
    99.  
    100.         if (!goright)
    101.         {
    102.             desiredLane--;
    103.  
    104.             if (desiredLane == -1)
    105.             {
    106.                 desiredLane = 0;
    107.             }
    108.         }
    109.         else
    110.         {
    111.             desiredLane++;
    112.  
    113.             if (desiredLane == 3)
    114.             {
    115.                 desiredLane = 2;
    116.             }
    117.         }
    118.     }
    119. }
    120.  
    121.  
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    usualy you have an terrain with a collider , also the player,

    with that you shouldnt go under your terrain
     
  3. Akashdj

    Akashdj

    Joined:
    Nov 5, 2017
    Posts:
    3
    I have downjump also, So cannot add terrain there.
    If we swipe down , the player will reach the ground and he should be back in position 3 of y axis.
     
  4. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    You can still use a collider on an invisible game object that acts as your ground
     
  5. Akashdj

    Akashdj

    Joined:
    Nov 5, 2017
    Posts:
    3
    I have downjump. If there is a collider then I cannot go down when I swipe down.

    if (MobileInput.Instance.SwipeDown)
    {
    jumpeddown = true;
    verticalvelocity = -jumpforce;
    // anim.SetTrigger("Jump");
    }