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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

how to add a jump/double jump to a free form directional mecanim controller with scripting

Discussion in 'Scripting' started by ultimateronsu17, Aug 21, 2015.

  1. ultimateronsu17

    ultimateronsu17

    Joined:
    May 30, 2015
    Posts:
    34
    Please help just stuck on this part on my script for my mecanim controller I need to add a jump / double jump to this


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Movement : MonoBehaviour {
    5.  
    6.     Animator anim;
    7.  
    8.     bool grounded = false;
    9.     public Transform groundCheck;
    10.     float groundRadius = 0.2f;
    11.     public LayerMask whatIsGround;
    12.  
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.      
    17.         anim = GetComponent<Animator> ();
    18.     }
    19.  
    20.     void FixedUpdate (){
    21.  
    22.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    23.         anim.SetBool ("Ground", grounded);
    24.  
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update () {
    29.  
    30.         float input_x = Input.GetAxisRaw ("Horizontal");
    31.         float input_z = Input.GetAxisRaw ("Vertical");
    32.  
    33.         bool IsWalking = (Mathf.Abs (input_x) + Mathf.Abs (input_z)) >0;
    34.         anim.SetBool ("IsWalking", IsWalking);
    35.         if (IsWalking)
    36.         {
    37.             anim.SetFloat ("x", input_x);
    38.             anim.SetFloat ("z", input_z);
    39.  
    40.             transform.position += new Vector3(input_x, 0f, input_z).normalized * Time.deltaTime;
    41.             if (grounded && Input.GetKeyDown (KeyCode.Space))
    42.             {
    43.                 anim.SetBool ("Ground",false);
    44.             }
    45.         }
    46.     }
    47. }
    Any suggestions?
     
  2. BudBroesky

    BudBroesky

    Joined:
    Nov 11, 2013
    Posts:
    15
    try this, it works perfectly for 2D games, but it should work for 3D as well.

    You'll need to create a float called "jumpHeight" to control... well I think it's pretty obvious xD.

    Code (CSharp):
    1. if (grounded && Input.GetKeyDown(Keycode.Space))
    2. {
    3.      anim.SetBool("Ground", false);
    4.      GetComponent<Rigidbody>().AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
    5. }
     
  3. ultimateronsu17

    ultimateronsu17

    Joined:
    May 30, 2015
    Posts:
    34
    OK ill give t a shot :)
     
  4. BudBroesky

    BudBroesky

    Joined:
    Nov 11, 2013
    Posts:
    15
    Let me know if it works!
     
  5. ultimateronsu17

    ultimateronsu17

    Joined:
    May 30, 2015
    Posts:
    34
    Also by the way It's for my 3d game so I'm trying to figure this out if you have another method I'm all ears :)
     
  6. BudBroesky

    BudBroesky

    Joined:
    Nov 11, 2013
    Posts:
    15
    Did that method not work?
     
  7. ultimateronsu17

    ultimateronsu17

    Joined:
    May 30, 2015
    Posts:
    34
    nah it gave me some errors
    here:
    error CS1503: Argument `#1' cannot convert `object' expression to type `UnityEngine.Vector3'
    The best overloaded method match for `UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)' has some invalid arguments
    Operator `*' cannot be applied to operands of type `UnityEngine.Vector2' and `bool'