I need to add a jump function and a gravity function to this script, but I am stuck.
I also want to make a maximum jump height.
Character.cs
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Character : MonoBehaviour {
//Movement Speed
public float moveSpeed = 50.0f;
public float sprintSpeed = 75.0f;
public float ySpeed = 20f;
private void Start()
{
}
private void Update()
{
//Movement
if (Input.GetKey("w"))
{
transform.Translate((Vector3.forward) * moveSpeed * Time.deltaTime);
}
if (Input.GetKey("a"))
{
transform.Translate((Vector3.left) * moveSpeed * Time.deltaTime);
}
if (Input.GetKey("s"))
{
transform.Translate((Vector3.back) * moveSpeed * Time.deltaTime);
}
if (Input.GetKey("d"))
{
transform.Translate((Vector3.right) * moveSpeed * Time.deltaTime);
}
//Sprint
if (Input.GetKeyDown(KeyCode.LeftShift)){
moveSpeed = sprintSpeed;
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
moveSpeed = 50.0f;
}
}
}
I want a different script to add the function of gravity, so I can add it to other objects in the future. Can you please help me?
Have you considered using Unity's built in Rigidbody Component to handle gravity, and different forces? This would probably be easier than writing it all yourself. https://docs.unity3d.com/ScriptReference/Rigidbody.html
Your answer
Follow this Question
Related Questions
Adding Gravity to a game object to make a black hole sucking effect. 1 Answer
How can I bind this script into the state of my camera? 2 Answers
WheelColliders Slip without end (with planet gravity, custom terrain mesh) 1 Answer
How to make a player instantly reach to a change in gravity? 2 Answers
Object refuses to collide 0 Answers