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 /
  • Help Room /
avatar image
0
Question by pjmoradiya · Apr 25, 2017 at 02:34 PM · c#unity5velocityangularvelocity

How do I measure the velocity of object in Z-direction?

I am new to unity.

I have made a bicycle moving and I am putting a speedometer for that. The idea I used is detecting the velocity of an object and transfer it into angular movement. But the problem is that the speedometer is detecting the direction in Y-Direction (Like if I remove the gravity from rigid body, the bicycle goes downward and the speedometer is showing the speed). Here I want to measure the velocity of bicycle win Z-Direction.

Here I am adding two scripts which I used for Bicycle to move and another one for speedometer I put in.

Please help me through this.

Thank You

C# Script : Bicycle using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerFinalDuplicate_2 : MonoBehaviour {

 public Animator anim;
 Rigidbody rb;
 public float speed = 50f;            
 public float rotationSpeed = 180f; 
 public float translationRate = 10f;


 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()
 {
     if (Input.GetKey (KeyCode.LeftShift)) {
         run = true;
     } else {
         run = false;
     }


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

     float translation = Input.GetAxis ("Vertical") * speed;
     translation *= Time.deltaTime;
     rb.AddForce (this.transform.forward * -translation*translationRate);



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

}

C# Script : Speedometer

Here I set the the image filling for measuring the velocity.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class SpeedImage : MonoBehaviour { public Rigidbody rb; // HERE I NEED TO DROP THE BICYCLE OBJECT FROM THE HIERARCHY IN THE INSPECTOR BY SELECTING THE IMAGE FROM CANVAS I ADDED. public float minVelocity = 0.0f; public float maxVelocity = 10.0f;

 private Image image;


 // Use this for initialization
 void Start () 
 {
     image = GetComponent<Image>();
 }
 
 // Update is called once per frame
 void Update () 
 {
     image.fillAmount = rb.velocity.magnitude/maxVelocity;

 }

}

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

307 People are following this question.

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

Related Questions

Take an average of the velocity and angular velocity of Parent over the last few frames and set directly on the Child Rigidbody at the moment the object is released. 0 Answers

how to make the sprite invinsible and reappear and how to call the sprite data from another script 0 Answers

spawning coins in an endless runner. pausing function for a specific time? 1 Answer

playing sound on certain condition on object 1 Answer

How to play a random audio clip from an array in C#? 1 Answer


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