Question by
Knuckles_69 · Apr 01, 2020 at 10:00 PM ·
c#rigidbody
how can I add Sprint into my Script ?,How can I add Sprint function ??
I want to add Sprint function in my script ,but I don't have any idea how to do it..
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BSP.RB
{
[RequireComponent(typeof(Rigidbody))]
public class Controller : MonoBehaviour
{
public float speed = 7.0f;
public float sprint = 15.0f;
public float jumpheight = 2f;
public float gravity = -9.81f;
public float grounddist = 0.2f;
public float dashdist = 5f;
public LayerMask ground;
public Rigidbody rigidbody;
public Vector3 input = Vector3.zero;
public bool isGrounded = true;
private Transform groundCheck;
private void Start()
{
rigidbody = this.GetComponent<Rigidbody>();
groundCheck = transform.GetChild(0);
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, grounddist, ground, QueryTriggerInteraction.Ignore);
if (Input.GetButtonDown("Jump") && isGrounded)
rigidbody.AddForce(Vector3.up * Mathf.Sqrt(jumpheight * -2f * Physics.gravity.y), ForceMode.VelocityChange);
if (Input.GetKeyDown(KeyCode.R)) //Dash
{
Vector3 dashVel = Vector3.Scale(transform.forward, dashdist * new Vector3((Mathf.Log(1f / (Time.deltaTime * rigidbody.drag + 1))
/ -Time.deltaTime), 0, (Mathf.Log(1f / (Time.deltaTime * rigidbody.drag + 1)) / -Time.deltaTime)));
rigidbody.AddForce(dashVel, ForceMode.VelocityChange);
}
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translation);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
}
Comment