Search Unity

How to make it when you jump you can only jump if you are touching something

Discussion in 'Scripting' started by NotSoLuckyDucky, Apr 1, 2019.

  1. NotSoLuckyDucky

    NotSoLuckyDucky

    Joined:
    Apr 16, 2018
    Posts:
    56
    I have a little random script I am messing with and I can't figure out how to make it ONLY jump when it is TOUCHING something.

    Here is my code:
    [/code]
     
  2. NotSoLuckyDucky

    NotSoLuckyDucky

    Joined:
    Apr 16, 2018
    Posts:
    56
    I also made a fun little Look script if you want to try it
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LookAim : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.        
    11.     }
    12.     [SerializeField] private Transform ObjectToLookAt;
    13.    
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         Vector3 DirectionToFace = ObjectToLookAt.localPosition - transform.localPosition;
    18.         transform.rotation = Quaternion.LookRotation(DirectionToFace);
    19.         Debug.DrawRay(transform.position, DirectionToFace, Color.red);
    20.         Quaternion targetRotation = Quaternion.LookRotation(DirectionToFace);
    21.  
    22.         transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime);
    23.     }
    24. }
    just place in the object you want to look at and BOOM
     
  3. NotSoLuckyDucky

    NotSoLuckyDucky

    Joined:
    Apr 16, 2018
    Posts:
    56
    sorry I forgot the code in the first part

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FireSpirit : MonoBehaviour
    6. {
    7.     public int jumpPower = 500;
    8.     public int speed = 1000;
    9.     [SerializeField] ParticleSystem jumpFX;
    10.  
    11.     Rigidbody rigidBody;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         rigidBody = GetComponent<Rigidbody>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {  
    21.         Jump();
    22.         Move();
    23.     }
    24.  
    25.     private void Move()
    26.     {
    27.         if (Input.GetKey(KeyCode.W))
    28.         {
    29.             rigidBody.AddRelativeForce(Vector3.forward * speed * Time.deltaTime);
    30.         }
    31.         if (Input.GetKey(KeyCode.A))
    32.         {
    33.             rigidBody.AddRelativeForce(Vector3.left * speed * Time.deltaTime);
    34.         }
    35.         if (Input.GetKey(KeyCode.S))
    36.         {
    37.             rigidBody.AddRelativeForce(Vector3.back * speed * Time.deltaTime);
    38.         }
    39.         if (Input.GetKey(KeyCode.D))
    40.         {
    41.             rigidBody.AddRelativeForce(Vector3.right * speed * Time.deltaTime);
    42.         }
    43.     }
    44.     private void Jump()
    45.     {
    46.        
    47.         {
    48.             if (Input.GetKeyDown(KeyCode.Space))
    49.             {
    50.                 rigidBody.AddRelativeForce(Vector3.up * jumpPower);
    51.             }
    52.         }
    53.     }
    54.  
    55. }
    56.