Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by pjmoradiya · Apr 22, 2017 at 01:03 AM · c#unity 5movementvelocitymeasure

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);
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sergio7888 · Apr 22, 2017 at 08:36 PM 1
Share

Use float speed = rb.velocity.magnitude;

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pjmoradiya · Apr 23, 2017 at 01:34 AM 0
Share

If I add this script in my script, then the bicycle stop moving.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges