How can I move my 2D car in 16 fixed angles like in Retro City Rampage
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnotherTestScript : MonoBehaviour {
Rigidbody2D body;
public float gears = 1f;
public float acceleration = 0f;
//Views:
public SpriteRenderer spr;
public Sprite view1;
public Sprite view2;
public Sprite view3;
public Sprite view4;
public Sprite view5;
public Sprite view6;
public Sprite view7;
public Sprite view8;
public Sprite view9;
public Sprite view10;
public Sprite view11;
public Sprite view12;
public Sprite view13;
public Sprite view14;
public Sprite view15;
public Sprite view16;
public void Awake()
{
body = GetComponent<Rigidbody2D>();
}
// Use this for initialization
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
float moveHorizontal = Input.GetAxisRaw("Horizontal");
float moveVertical = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0.0f);
body.velocity = movement * gears;
float h = gameObject.transform.position.x;
float v = gameObject.transform.position.y;
//Vector2 inputVector = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
//body.velocity = inputVector * 3 ;
if (Input.GetKeyDown(KeyCode.A))
{
spr.sprite = view13;
//h -= 1f;
}
if (Input.GetAxisRaw("Horizontal") < -0.5f && Input.GetAxisRaw("Vertical") > 0.5)
{
spr.sprite = view15;
}
if (Input.GetKey(KeyCode.W) == true)
{
spr.sprite = view1;
}
if (Input.GetKey(KeyCode.S) == true)
{
spr.sprite = view9;
}
if (Input.GetAxisRaw("Horizontal") > 0.5f)
{
spr.sprite = view5;
}
}
}
This is my script. The sprite variables called 'view1', 'view2', 'view3', etc are where the car individual 16 isometric pixel sprites are stores. I wanted the cars to drive like they do in retro city rampage. for those of you who have not played retro city rampage, here is a video on the game from NerdCubed. Link: https://www.youtube.com/watch?v=56CrKrbRuIc
I want the driving to be like the driving in the game with individual angle movements and the controls will be like WSAD or the arrow keys. I don't know how to do this. Can you please help.
Answer by jdean300 · Feb 23, 2017 at 12:27 AM
What you can do there is have a simulated car angle that moves continuously based on the input, like a typical racing game. Now take that simulated angle and use the following code to convert it into an integer direction which you use to select a sprite and travel direction.
public static int GetDirection(float angle)
{
angle /= (360f / 16f);
return (Mathf.RoundToInt(angle)) % 16;
}
Your answer
Follow this Question
Related Questions
Isometric Movent Issue 0 Answers
Isometric movement for MoveTowards 0 Answers
Player facing mouse with orthographic isometric camera 0 Answers
I can't make my character move 0 Answers
Lerp ignores collision and allows player to move through a wall. 0 Answers