Question by
nightsharks · Jan 16, 2018 at 08:33 PM ·
car game
Simple vehicle script
I need help creating a simple vehicle script for a little top down game I'm working on. The default Unity Asset CarController script is way too detailed for what I need- all I need is to move the car forward, back, and turn correctly with directional arrows or WSAD keys. No wheels or lights or anything like that. I run into a lot of issues when turning the car.
This is what I have after looking at some tutorials:
using System.Collections; using System.Collections.Generic; using UnityEngine;
[System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; }
public class PlayerController : MonoBehaviour {
public float speed;
public Boundary boundary;
public float rotation;
private Rigidbody rb;
public float velocity;
public float RotateSpeed;
public float maxVelocityX;
public float maxVelocityZ;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
// rb.velocity = movement * speed;
var maxVelocityX = 2f;
var maxVelocityZ = 2f;
rb.AddForce(movement * speed * Time.deltaTime * 1000);
rb.velocity = new Vector3
(
Mathf.Clamp(rb.velocity.x, -maxVelocityX, maxVelocityX),
0.0f,
Mathf.Clamp(rb.velocity.z, -maxVelocityX, maxVelocityZ)
);
this.transform.LookAt(this.transform.position + (rb.velocity).normalized, Vector3.up);
}
Comment
Your answer
Follow this Question
Related Questions
Best way to do this 0 Answers
Progressive lerp?? (reupload) 0 Answers