- Home /
Restricted movement relative to a grid with turns
I want to move on a grid using ProGrids. I want to be only able to move 3 spaces at a time. here is my movement code so far.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour { public float speedX = 100; public float speedY = 100; public Rigidbody2D rb; public Vector2 destination; public Vector2 place; public Vector2 journey; public Vector2 gap;
void Start () {
rb = GetComponent<Rigidbody2D>();
place = rb.transform.position;
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
destination = Camera.main.ScreenToWorldPoint (Input.mousePosition);
journey.x = destination.x - place.x;
journey.y = destination.y - place.y;
rb.velocity = new Vector2 (journey.normalized.x * speedX * Time.deltaTime, journey.normalized.y * speedY * Time.deltaTime);
}
}
void LateUpdate (){
place = rb.transform.position;
gap.x = Mathf.Abs (destination.x) - Mathf.Abs (place.x);
gap.y = Mathf.Abs (destination.y) - Mathf.Abs (place.y);
if (.05 > Mathf.Abs (gap.x))
if (.05 > Mathf.Abs (gap.y))
rb.velocity = new Vector2 (0,0);
}
}
Answer by HexUltra · Dec 03, 2018 at 02:13 AM
[link text][1]
Here is the entire code [1]: /storage/temp/128793-movement.txt
Your answer
![](https://koobas.hobune.stream/wayback/20220612183218im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unet wiever bug happened after fully configuring playfab 0 Answers
MissingReferenceException: 0 Answers
Use Sprites as Score 1 Answer
Check whether any collision has occured to spawned game objects 0 Answers
Image as boundary 1 Answer