- Home /
Question by
pjmoradiya · Apr 19, 2017 at 09:28 AM ·
unity 5rotationbuttonturning
How can I turn the direction of bicycle in unity with animations.
I have used below C# script for bicycle to run with the animations. With this script I cannot turn into left and right direction. I can only move towards the left and right direction. Can anyone please help me to add some script to turn the bicycle into the scene
Thank You
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);
}
}
Comment