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 jeremydulong · Jan 31, 2019 at 09:29 PM · movementcamera-movement

Moving smoothly around the bounds of a circle

alt text

This is the image of what I'm trying to achieve, in a 3D space. I want the object to follow my finger within the green zone, but stay on the edge of the green zone if I move my finger outside of it. I have achieved this with the code below, but when moving my finger around the red zone a lot of jitters and clipping occurs as the object keeps snapping back within it's bounds. My main camera is attached to the moving object so it's important that I eliminate the jitters. How can I smooth this out?

 public class Player : MonoBehaviour
 {
     public Camera movementCam;
     readonly float radius = 0.45f;
     readonly float speed = 3f;
     Ray firstTouchPos;
     Vector2 playerPos;
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0)) {
             firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
             playerPos = transform.position;
         }
 
         if (Input.GetMouseButton(0)) {
             Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
             Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
             float distance = Vector3.Distance(transform.position, Vector3.zero);
             transform.position = distance >= radius ? (Vector3)(direction.normalized * radius) : (Vector3)(playerPos + direction * speed);
         }
     }
 }
 


55651-radius-touch.png (48.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cornelis-de-Jager · Jan 31, 2019 at 09:40 PM

You can achieve this by using Lerp or Slerp. See example below

 public Camera movementCam;
      readonly float radius = 0.45f;
      readonly float speed = 3f;
      Ray firstTouchPos;
      Vector2 playerPos;
  
      void Update()
      {
          if (Input.GetMouseButtonDown(0)) {
              firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
              playerPos = transform.position;
          }
  
          if (Input.GetMouseButton(0)) {
              Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
              Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
              float distance = Vector3.Distance(transform.position, Vector3.zero);
              var targetPosition = distance >= radius ? (Vector3)(direction.normalized * radius) : (Vector3)(playerPos + direction * speed);
              transform.position = Vector3.Lerp (transform.position, targetPosition, 1f); 
          }
      }
  
  

However, I would recommend you put the slerp function outside of the MouseButtonDown. That way you can add a smoother following/delay

 readonly float radius = 0.45f;
      readonly float speed = 3f;
      Ray firstTouchPos;
      Vector2 playerPos;
      Vector3 targetPosition;
      float followDelay = 2f;
      void Update()
      {
          if (Input.GetMouseButtonDown(0)) {
              firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
              playerPos = transform.position;
          }
  
          if (Input.GetMouseButton(0)) {
              Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
              Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
              float distance = Vector3.Distance(transform.position, Vector3.zero);
              targetPosition = distance >= radius ? (Vector3)(direction.normalized * radius) : (Vector3)(playerPos + direction * speed);
             
          }
          
           transform.position = Vector3.Lerp (transform.position, targetPosition, followDelay); 
      }
  
Comment
Add comment · Show 1 · Share
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 jeremydulong · Jan 31, 2019 at 09:54 PM 0
Share

Thank you @Cornelis-de-Jager for the response. Adding a Lerp is definitely a good idea and will smooth out the movement of the camera while in the bounds, but the jitters I'm seeing are caused when holding my finger in the red zone out of the players circle bounds. Ins$$anonymous$$d of being "stuck" in the bounds the player is trying to continue and then being positioned back within the bounds, causing jitters. I'm looking for a way to limit the movement of the player within the bounds without having to reset it's position.

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

160 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to select one character and play it's animation? 1 Answer

Help with player movement and jumping 0 Answers

how to rotate velocity/ tying movement to camera 0 Answers

Unity Movement 1 Answer

Panning and Rotating a Camera 0 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