Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ijidau · Nov 11, 2015 at 01:04 AM 1
Share

Did you have a question specific to this script?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges