- Home /
Question by
Koraemon · Nov 11, 2015 at 01:00 AM ·
pathfollowingpaths
How should I go about this? Creating Path with mouse/touch, upon release, follow the movement
I am trying to create a way to make my character follow an outlined path that the player can draw. The player can draw the path and after the finger/mouse has been lifted off/unclicked, the player will follow the path. First time ever attempting something like this and I am fairly new to this so any help would be great,
I will be actively searching the forum in search of a solution :P.
Thanks.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class SwipeControls : MonoBehaviour
{
private CharacterAnimation anim;
private List<Vector2> touchPositions = new List<Vector2>();
private int sizeOfArray;
private Vector3 DestinationPosition;
private float DestinationDistance = 0F;
private float speed = 10F;
private float minMove = 0.3f;
private float maxMove = 500.0f;
private bool mouseHeld;
private float delay = 0.1F;
private float count = 0F;
void Start()
{
anim = GetComponent<CharacterAnimation>();
}
void Update()
{
sizeOfArray = touchPositions.Count;
}
void FixedUpdate()
{
MoveControl();
swipeDetect();
}
void OnGUI()
{
GUIStyle myStyle = new GUIStyle();
myStyle.fontSize = 20;
GUI.Label(new Rect(20, 20, 100, 100), "Destination Distance " + DestinationDistance.ToString(), myStyle);
GUI.Label(new Rect(20, 50, 100, 100), "Size Of Array " + touchPositions.Count, myStyle);
}
void OnEnable()
{
DestinationPosition = transform.position;
}
private void swipeDetect()
{
if (Input.touchCount > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
touchPositions.Clear();
}
if (t.phase == TouchPhase.Moved)
{
}
if (t.phase == TouchPhase.Ended)
{
RotatePlayer();
}
if (Time.time > count)
{
touchPositions.Add(t.position);
count = Time.time + delay;
}
}
DestinationPosition.y = transform.position.y;
DestinationDistance = Vector3.Distance(DestinationPosition, transform.position);
}
private void MoveControl()
{
MovePlayer();
if (Input.GetMouseButton(0) && mouseHeld == false)
{
touchPositions.Clear();
mouseHeld = true;
}
if (Input.GetMouseButtonUp(0) && mouseHeld == true)
{
RotatePlayer();
mouseHeld = false;
}
if (mouseHeld != false)
{
if (Time.time > count)
{
touchPositions.Add(Input.mousePosition);
count = Time.time + delay;
}
}
}
private void MovePlayer()
{
if (DestinationDistance >= minMove && DestinationDistance <= maxMove)
{
Debug.DrawLine(DestinationPosition, transform.position, Color.cyan);
GetComponent<Rigidbody>().MovePosition(GetComponent<Rigidbody>().position + transform.forward * speed * Time.deltaTime);
anim._animRun = true;
}
else
{
anim._animRun = false;
if(mouseHeld != true)
{
touchPositions.Clear();
DestinationPosition = transform.position;
}
}
}
private void RotatePlayer()
{
for (int i = 0; i < sizeOfArray; i++)
{
if (DestinationDistance <= 0.5F)
{
DestinationPosition = touchPositions[i];
}
}
Ray ray = Camera.main.ScreenPointToRay(DestinationPosition); // Set ray to the position of your Destination
Plane playerPlane = new Plane(Vector3.up, transform.position); // Create a plane for the raycast
float hitdist = 0.0f; // set a float for the position of your click
if (playerPlane.Raycast(ray, out hitdist)) // If the Raycast has hit something (in this case, the mouse position) then continue
{
Vector3 targetPoint = ray.GetPoint(hitdist); // Set a Vector3 for position clicked
DestinationPosition = targetPoint; // Set destination position to position clicked
GetComponent<Rigidbody>().MoveRotation(Quaternion.LookRotation(DestinationPosition - transform.position));
}
}
private IEnumerator clearList(float delay)
{
yield return new WaitForSeconds(delay);
touchPositions.Clear();
DestinationPosition = transform.position;
}
}
Comment
Your answer
Follow this Question
Related Questions
Is there a way to save a 2D characters path and replay it on respawn? 0 Answers
My "Smooth Rotation" Jerks around randomly when switching between points. 0 Answers
Curve Path Definition 0 Answers
Import movement animation path from 3Dmax 1 Answer
How to get object to follow a curved path to reach a moving destination? 1 Answer