- Home /
How can I turn the direction of bicycle in Unity?
Hi I am new to the Unity and I am making Bicycle project in my school. I am facing a problem regarding turning the bicycle. I am attaching the YouTube link for the video which I made in which you can see the problem. I am also copying the C# Script below. Please help me through this.
YouTube Video Link : https://youtu.be/Xbd4ejR_4cY
Thank You
C# Script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class player : MonoBehaviour { public Animator anim; public Rigidbody rbody; private bool run;
private float inputH;
private float inputV;
void FixedUpdate(){
Rotation ();
}
private float wantedYRotation;
private float currentYRotation;
private float rotateAmountByKeys = 2.5f;
private float rotationYVelocity;
void Rotation(){
if (Input.GetKey(KeyCode.LeftArrow))
{
wantedYRotation -= rotateAmountByKeys;
}
if (Input.GetKey(KeyCode.RightArrow))
{
wantedYRotation += rotateAmountByKeys;
}
currentYRotation = Mathf.SmoothDamp (currentYRotation, wantedYRotation, ref rotationYVelocity, 0.25f);
}
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator>();
rbody = GetComponent<Rigidbody> ();
run = false;
}
// Update is called once per frame
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*100f*Time.deltaTime;
float moveZ = inputV*100f*Time.deltaTime;
if (moveZ <= 0f)
{
moveX = 0f;
}
else if (run)
{
moveX *= 2f;
moveZ *= 2f;
}
rbody.velocity = new Vector3 (moveX, 0f, moveZ);
}
}
Answer by jackishere · Apr 19, 2017 at 03:08 PM
have you applied the rotation?
transform.rotation=Vector3.up*currentYRotation;
Answer by pjmoradiya · Apr 20, 2017 at 06:30 PM
Thank you for your response. I am new to the Unity so I exactly don't know how to use rotation in this script. Can you please give me some idea regarding this?
Answer by thaMorganic · Apr 20, 2017 at 08:04 AM
Hmm, not quite sure what you wanna do, but try this:-
transform.rotation = Quaternion.Euler (X, Y, Z);
Note: Replace X,Y, Z with the rotation you want on every axis.
If you want to make it rotate continuously on a specific axis, use this:-
Quaternion rot = Quaternion.Euler (0, rotationSpeed, 0);
transform.rotation *= rot;
Note: Put rotationSpeed in the axis you wanna rotate, like this
X-axis > Quaternion.Euler (rotationSpeed, 0 , 0);
Y-axis > Quaternion.Euler (0, rotationSpeed, 0);
And so on...
Your answer
Follow this Question
Related Questions
how do i make first person character rotate left and right along with camera? 0 Answers
Can't do Quaternion.AngleAxis twice. 1 Answer
Flip over an object (smooth transition) 3 Answers
Scripting Help(C#): Make object face a direction based on rotation of another 1 Answer
How to move camera up and down 1 Answer