- Home /
Question by
tulloch100 · Jul 31, 2017 at 05:54 PM ·
script.rotatecar
Rotate object only on player input - Script help!!!!!!!!
I'm creating a kart game with the go kart being pedal powered and so far I have a script that allows me to rotate the pedals when I start the game and that's all good.
but what I want to do is to be able to only have them rotate once I start moving the kart.
This is the pedal script I have working at the moment
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PedalScript : MonoBehaviour {
public float rotationSpeed = 10f;
public Transform leftPedal, rightPedal;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Rotate (Vector3.right * Time.deltaTime * rotationSpeed);
leftPedal.rotation = Quaternion.LookRotation (Vector3.right);
rightPedal.rotation = Quaternion.LookRotation (Vector3.right);
}
}
and this is the kart movement script
using System.Collections;
using UnityEngine;
public class Car : MonoBehaviour
{
public float maxTorque = 5000f;
public float speed;
public Transform centerofMass;
public WheelCollider[] wheelColliders = new WheelCollider[4];
public Transform[] tireMeshes = new Transform[4];
private Rigidbody m_rigidBody;
void Start()
{
m_rigidBody = GetComponent<Rigidbody>();
m_rigidBody.centerOfMass = centerofMass.localPosition;
}
void Update()
{
UpdateMeshesPositions();
}
void FixedUpdate()
{
float steer = Input.GetAxis ("Horizontal");
float accelrate = Input.GetAxis ("Vertical");
float finalAngle = steer * 45f;
wheelColliders [0].steerAngle = finalAngle;
wheelColliders [1].steerAngle = finalAngle;
for (int i = 0; i < 4; i++)
{
wheelColliders[i].motorTorque = accelrate * maxTorque;
}
}
void UpdateMeshesPositions()
{
for(int i = 0; i < 4; i++)
{
Quaternion quat;
Vector3 pos;
wheelColliders[i].GetWorldPose(out pos, out quat);
tireMeshes[i].position = pos;
tireMeshes[i].rotation = quat;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Rotate Object only on player input 0 Answers
Why when using LookAt to make the turret facing the target it's facing the other direction ? 1 Answer
Change the default car controller 2 Answers
How to Attach Steering wheel script to a car moment script 2 Answers
Scripting control of rotation on collision exit of terrain 2 Answers