Question by
Garharr · Mar 02, 2019 at 03:29 AM ·
jumpjumpingjumping object
Need help with jumping
https://youtu.be/Ty9dmOlHBxg I am having trouble with my unity game. I want to add a jump button, but I don't know how to make the character move up smoothly without typing it in the void update. When it is in the void update, it keeps on registering the Space button so it keeps on repeating the command (I guess?). I tried using Input.GetKeyDown (Instead of Input.GetKey), but it then teleports it up instead of a smooth transition. What I need is something where it would only register the space bar hit once, then have it smoothly go up then down.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasicMovement : MonoBehaviour {
public PickupScript pickup;
Rigidbody rb;
public Animator animator;
void Start () {
rb = GetComponent<Rigidbody>();
animator = GetComponent<Animator>();
}
void Update () {
float MouseX = Input.mousePosition.x * 0.01f;
float MouseY = Input.mousePosition.y * 0.01f;
var move = new Vector3((Input.GetAxis("Horizontal") * 2), 0, 0);
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * 2 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.back * 2 * Time.deltaTime);
}
if (Input.GetKey(KeyCode.Space))
{
transform.Translate (Vector3.up * 5 * Time.deltaTime);
}
animator.SetFloat("Horizontal", move.x);
animator.SetFloat("Magnitude", move.magnitude);
transform.position = transform.position + move * Time.deltaTime;
}
}
Comment