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();
rb = this.GetComponent();
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);
}
}
↧