How can I add sprint function into my Script?
The increase speed float is sprint but how can i add the sprint into my fixedupdate??
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MovementController : MonoBehaviour
{
public float movementSpeed = 3f;
public float IncreaseSpeed = 6f;
public float rotationSpeed = 90f;
public float cameraDistance = 5f;
public float cameraHeight = 10f;
Transform mainCamera;
Vector3 cameraOffset;
void Start()
{
cameraOffset = new Vector3(0f, cameraHeight, -cameraDistance);
mainCamera = Camera.main.transform;
MoveCamera();
}
void FixedUpdate()
{
float rotation = Input.GetAxis("Horizontal");
float movement = Input.GetAxis("Vertical");
GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;
transform.Rotate(Vector3.up * rotationSpeed * rotation * Time.deltaTime);
MoveCamera();
}
void MoveCamera()
{
mainCamera.position = transform.position;
mainCamera.rotation = transform.rotation;
mainCamera.Translate(cameraOffset);
mainCamera.LookAt(transform);
}
}
void FixedUpdate()
{
float rotation = Input.GetAxis("Horizontal");
float movement = Input.GetAxis("Vertical");
if (Input.GetButton("IncreaseSpeed"))
{
GetComponent<Rigidbody>().velocity = transform.forward * increaseSpeed * movement;
}
if (Input.GetButtonUp("IncreaseSpeed"))
{
GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;
}
transform.Rotate(Vector3.up * rotationSpeed * rotation * Time.deltaTime);
$$anonymous$$oveCamera();
}
Answer by coughlinw97 · Jan 27, 2017 at 10:21 PM
I would just add a while loop using whatever input you want the sprint to be associated with, i like left shift as my sprint, then set the velocity to your sprint speed.
while (Input.GetKey(KeyCode.LeftShift)) { GetComponent().velocity = transform.forward increaseSpeed movement; }
You may also have to add an if statement for setting the speed back to normal if it doesnt automatically
if (Input.GetKeyUp(KeyCode.LeftShift)) { GetComponent().velocity = transform.forward movementSpeed movement; }
This caused my game to crash, do i put the if statement within the loop statement so it would look like this. cause this doesnot work for me.
while (Input.GetButton("IncreaseSpeed"))
{
GetComponent<Rigidbody>().velocity = transform.forward * increaseSpeed * movement;
if (!Input.GetButton("IncreaseSpeed"))
{
GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;
}
change the while loop to an if statement, and keep the second if statement outside of the first one. This works for me.
if (Input.GetButton("IncreaseSpeed"))
GetComponent<Rigidbody>().velocity = transform.forward * increaseSpeed * movement;
if (Input.Get$$anonymous$$eyUp("IncreaseSpeed"))
GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;
the run works but regular movement dont...
if (Input.GetButton("IncreaseSpeed"))
{
GetComponent<Rigidbody>().velocity = transform.forward * increaseSpeed * movement;
}
if (Input.GetButtonUp("IncreaseSpeed"))
{
GetComponent<Rigidbody>().velocity = transform.forward * movementSpeed * movement;
}
Your answer
Follow this Question
Related Questions
How can I move an object in the direction another object is facing. 1 Answer
Freezing an object and keeping it's velocity 1 Answer
I added Rigidbody to my cube and now it goes super fast when i move! 0 Answers
Question about NavMesh and Rigidbody 0 Answers
How to apply a force at certain point of an object? 0 Answers