- Home /
How to measure Speed?
Hello!
I am new to the Unity and I am making a bicycle project in which I want to measure the speed of the bicycle. Somehow I manage to make the script by online forum and after trial and trial it worked.
I am attaching my C# script for movement of the bicycle.
PLEASE HELP ME TO SORT OUT MY REQUIREMENT. What script should I put and where to put?
C# Script. using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerFinalDuplicate : MonoBehaviour {
public Animator anim;
Rigidbody rb;
public float speed = 50f;
public float rotationSpeed = 180f;
private bool run;
private float inputH;
private float inputV;
private string m_MovementAxisName;
private string m_TurnAxisName;
private float m_MovementInputValue;
private float m_TurnInputValue;
private void OnEnable ()
{
m_MovementInputValue = 0f;
m_TurnInputValue = 0f;
}
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
rb = this.GetComponent<Rigidbody>();
run = false;
m_MovementAxisName = "Vertical";
m_TurnAxisName = "Horizontal";
}
// Update is called once per frame
private void Update()
{
//FOR ANIMATION
if (Input.GetKey (KeyCode.LeftShift)) {
run = true;
} else {
run = false;
}
//FOR ANIMATION
inputH = Input.GetAxis ("Horizontal");
inputV = Input.GetAxis ("Vertical");
anim.SetFloat ("inputH", inputH);
anim.SetFloat ("inputV", inputV);
anim.SetBool ("run", run);
float moveX = inputH * speed * Time.deltaTime;
float moveZ = inputV * speed * Time.deltaTime;
if (moveZ <= 0f) {
moveX = 0f;
}
else if (run)
{
moveX *= 2f;
moveZ *= 2f;
}
// Store the player's input and make sure the audio for the engine is playing.
m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
m_TurnInputValue = Input.GetAxis (m_TurnAxisName);
}
private void FixedUpdate()
{
// Move and turn.
Move ();
Turn ();
}
private void Move()
{
// Adjust the position of the tank based on the player's input.
Vector3 movement = transform.forward * -m_MovementInputValue * speed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
}
private void Turn()
{
// Adjust the rotation of the tank based on the player's input.
float turn = m_TurnInputValue * rotationSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler (0f, turn, 0f);
rb.MoveRotation (rb.rotation * turnRotation);
}
}
Answer by Kishotta · Apr 22, 2017 at 08:00 PM
You can add something like this in:
Vector3 PreviousFramePosition = Vector3.zero; // Or whatever your initial position is
float Speed = 0f;
void Update () {
// All the update stuff
// ...
float movementPerFrame = Vector3.Distance (PreviousFramePosition, transform.position) ;
Speed = movementPerFrame / Time.deltaTime;
PreviousFramePosition = transform.position;
}
If I add this script in my script, then the bicycle stop moving.
Answer by pjmoradiya · Apr 22, 2017 at 11:20 PM
Thank you for your reply and answer. I am sorry to add in the first question that I want to insert the speedometer so that is why I want to measure speed. Let me use this script and see that id this will work for me to integrate the speedometer. If you have another way to add speedometer please tell me in my script. Thank You
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
How do I fix collisions while using this PlayerController script? 1 Answer
Player slows down when jumping/velocity changes 1 Answer
Carry forward velocity against mouse movements 1 Answer
How to make bullets moving proportionally faster than player? 4 Answers