Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 $$anonymous$$ · Jun 04, 2012 at 02:43 PM · movementai

Making a moving gameobject approach and stop at a target (arrival behaviour)

Hello everyone :)

Introduction

I am currently trying to create a script which controls point and click behaviour for a GameObject.

In essence, the player will - in the end - control a hot air balloon by simply clicking on the screen, whereby the object will start to accelerate and move toward the position of the click. When at a specific distance of the click position, the hot air balloon should then start to accelerate in the opposite direction (thus decelerating) and come to a complete halt exactly at the position of the click.

I have already created a fully functional and commented script (found at the bottom of this post), which simply moves a GameObject towards the position of a click at a constant speed, and then when within a specific distance, starts to slow down until stopping at the point.

I have also created a separately and fully functional script which moves toward the position of a click while accelerating (getting that kind of asteroids thrusting behaviour). This can also be found at the bottom of the post.

The Problem

Now the problem that I'm facing is that I've been working quite a while now to find a solution where you can take these two behaviours and implement them as one working script, that is, the hot air balloon have the accelerating behaviour while also slowing down and stopping at a target position, exactly like this visual example found here:

Visual Arrival Behaviour Demonstration

The Question

My question then becomes:

how can you make the arrival behaviour not only using constant speeds, but with acceleration included in the equation as well?

Script attachments

Point and click movement at constant speeds with arrival behaviour

 using UnityEngine;
 using System.Collections;

 public class PlayerControl : MonoBehaviour 
 {
 // Fields
 Transform cachedTransform;
 Vector3 currentMovementVector;
 Vector3 lastMovementVector;
 Vector3 currentPointToMoveTo;
 Vector3 velocity;
 int movementSpeed;

 Vector3 clickScreenPosition;
 Vector3 clickWorldPosition;
 
 float slowingDistance = 8.0f;
 
 Vector3 desiredVelocity;
 
 float maxSpeed = 1000.0f;
 
 // Use this for initialization
 void Start () 
 {
     cachedTransform = transform;
     currentMovementVector = new Vector3(0, 0);
     movementSpeed = 50;
     currentPointToMoveTo = new Vector3(0, 0);
     velocity = new Vector3(0, 0, 0);
 }
 
 // Update is called once per frame
 void Update () 
 {
     // Retrive left click input
     if (Input.GetMouseButtonDown(0))
     {
         // Retrive the click of the mouse in the game world
         clickScreenPosition = Input.mousePosition;
         
         clickWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(clickScreenPosition.x, clickScreenPosition.y, 0));
         currentPointToMoveTo = clickWorldPosition;
         
         currentPointToMoveTo.z = 0;
         
         // Calculate the current vector between the player position and the click
         Vector3 currentPlayerPosition = cachedTransform.position;
         
         // Find the angle (in radians) between the two positions (player position and click position)
         float angle = Mathf.Atan2(clickWorldPosition.y - currentPlayerPosition.y, clickWorldPosition.x - currentPlayerPosition.x);
         
         // Find the distance between the two points
         float distance = Vector3.Distance(currentPlayerPosition, clickWorldPosition);
         
         // Calculate the components of the new movemevent vector
         float xComponent = Mathf.Cos(angle) * distance;
         float yComponent = Mathf.Sin(angle) * distance;
          
         // Create the new movement vector
         Vector3 newMovementVector = new Vector3(xComponent, yComponent, 0);
         newMovementVector.Normalize();
         
         currentMovementVector = newMovementVector;
     }
     
     float distanceToEndPoint = Vector3.Distance(cachedTransform.position, currentPointToMoveTo);
     
     Vector3 desiredVelocity = currentPointToMoveTo - cachedTransform.position;

     desiredVelocity.Normalize();
     
     if (distanceToEndPoint < slowingDistance)
     {
         desiredVelocity *= movementSpeed * distanceToEndPoint/slowingDistance;
     }
     else
     {
         desiredVelocity *= movementSpeed;
     }
         
     Vector3 force = (desiredVelocity - currentMovementVector);
     currentMovementVector += force;

     cachedTransform.position += currentMovementVector * Time.deltaTime;
 }
 }

Point and click movement using acceleration but no arrival behaviour

 using UnityEngine;
 using System.Collections;

 public class SimpleAcceleration : MonoBehaviour 
 {
 Vector3 velocity;
 Vector3 currentMovementVector;
 
 Vector3 clickScreenPosition;
 Vector3 clickWorldPosition;
 
 Vector3 currentPointToMoveTo;
 Transform cachedTransform;
 
 float maxSpeed;
 
 // Use this for initialization
 void Start () 
 {
     velocity = Vector3.zero;
     currentMovementVector = Vector3.zero;
     cachedTransform = transform;
     maxSpeed = 100.0f;
 }
 
 // Update is called once per frame
 void Update () 
 {
     // Retrive left click input
     if (Input.GetMouseButtonDown(0))
     {
         // Retrive the click of the mouse in the game world
         clickScreenPosition = Input.mousePosition;
         
         clickWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(clickScreenPosition.x, clickScreenPosition.y, 0));
         currentPointToMoveTo = clickWorldPosition;
         
         // Reset the z position of the clicking point to 0
         currentPointToMoveTo.z = 0;
         
         // Calculate the current vector between the player position and the click
         Vector3 currentPlayerPosition = cachedTransform.position;
         
         // Find the angle (in radians) between the two positions (player position and click position)
         float angle = Mathf.Atan2(clickWorldPosition.y - currentPlayerPosition.y, clickWorldPosition.x - currentPlayerPosition.x);
         
         // Find the distance between the two points
         float distance = Vector3.Distance(currentPlayerPosition, clickWorldPosition);
         
         // Calculate the components of the new movemevent vector
         float xComponent = Mathf.Cos(angle) * distance;
         float yComponent = Mathf.Sin(angle) * distance;
          
         // Create the new movement vector
         Vector3 newMovementVector = new Vector3(xComponent, yComponent, 0);
         newMovementVector.Normalize();
         
         currentMovementVector = newMovementVector;
     }
     
     // Calculate velocity
     velocity += currentMovementVector * 2.0f * Time.deltaTime;
     
     // If the velocity is above the allowed limit, normalize it and keep it at a constant max speed when moving (instead of uniformly accelerating)
     if (velocity.magnitude >= (maxSpeed * Time.deltaTime))
     {
         velocity.Normalize();
         velocity *= maxSpeed * Time.deltaTime;
     }
     
     // Apply velocity to gameobject position
     cachedTransform.position += velocity;
 }
  }
Comment
Add comment
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

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

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Enemy Damage 2 Answers

AI using Javascript 1 Answer

Enemies Moving Through Terrain Help 1 Answer

movement script 2 Answers


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