Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to make movement of gameobject smoother along the boundary

Discussion in 'Scripting' started by Zagreus0, Jul 19, 2023.

  1. Zagreus0

    Zagreus0

    Joined:
    Jan 22, 2023
    Posts:
    5
    Code (CSharp):
    1. float radius = 400; //radius of *black circle*
    2. Vector3 centerPosition = transform.localPosition; //center of *black circle*
    3. float distance = Vector3.Distance(newLocation, centerPosition); //distance from ~green object~ to *black circle*
    4.  
    5. if (distance > radius) //If the distance is less than the radius, it is already within the circle.
    6. {
    7. Vector3 fromOriginToObject = newLocation - centerPosition; //~GreenPosition~ - *BlackCenter*
    8. fromOriginToObject *= radius / distance; //Multiply by radius //Divide by Distance
    9. newLocation = centerPosition + fromOriginToObject; //*BlackCenter* + all that Math
    10. }
    I tried this code block to restrain my game object's movement to a certain radius and it does work but when i move the object along the boundary or circumference, it jitters and isn't as smooth. Can anyone guide me so i can make it move smoother?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1. float radius = 400; //radius of *black circle*
    2. Vector3 centerPosition = transform.localPosition; //center of *black circle*
    3. float distance = Vector3.Distance(newLocation, centerPosition); //distance from ~green object~ to *black circle*
    4.    
    5. if (distance > radius)
    6. {
    7.     Vector3 direction=(newLocation-centerPosition).normalized;
    8.     newLocation=centerPosition+direction*radius;
    9. }
     
  3. Zagreus0

    Zagreus0

    Joined:
    Jan 22, 2023
    Posts:
    5
    thanks for helping but this still makes the object's movement jittery, i need it to move along the boundary like it would along a collider, smoothly. Do you have any advise as to how i can do that or sources that i can learn from?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Local Position isn't the position of a specific object so it's unclear why you're using local position there, just use position unless you are absolutely sure you want the relevative position from it's parent which is what local position is.

    Also, can we assume here that this is all just transform work and you've not neglected to mention that you're using physics? I mention this because you don't drive physics by modifying Transform.

    Also, what else changes position? It's one thing constraining position but if you do that only after it's been rendered at an unconstrained position then it'll be visibly jumping between a bad and good position.

    Need more info.
     
    Zagreus0 likes this.
  5. Zagreus0

    Zagreus0

    Joined:
    Jan 22, 2023
    Posts:
    5
    sorry i'm new and a little overwhelmed with everything, i am using physics to move the gameobject and the centre is a child transform that also moves as the player moves.

    the script for moving the game object:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class move2 : MonoBehaviour
    6. {
    7.     private float speed = 80f;
    8.     private Vector2 movement;
    9.  
    10.     [SerializeField] private Rigidbody2D rb;
    11.  
    12.     void Update()
    13.     {
    14.         if (Input.GetMouseButton(0))
    15.         {
    16.             movement.x = Input.GetAxis("Mouse X");
    17.             movement.y = Input.GetAxis("Mouse Y");
    18.             rb.MovePosition(rb.position + (speed * Time.deltaTime* movement.normalized));
    19.         }
    20.     }
    21. }
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Well MovePosition happens when the simulation runs which isn't per-frame but is, by default, during the FixedUpdate. Immediately after the above call, it hasn't moved. You'll need to read up on this.

    If you're asking to move to a position and you know it's going to break the circle constraint you want then that's the bad part.

    When you calculate the position you want to move to, ensure it's within the circle constraint so calculate where you want to move to:
    Code (CSharp):
    1. var newPosition = rb.position + (speed * Time.deltaTime* movement.normalized)
    ... then adjust "newPosition" to always be constrained by the circle. When you've done that, issue the MovePosition.

    You could always use a DistanceJoint2D on this body connected to the center of the circle and then the simulation will ensure you stay at that distance.

    Side note: MovePosition is meant to be used by Kinematic bodies but if you're using it on a Dynamic body then any collisions you have will also move it and potentially break your circle constraint.

    Side note 2: Don't use Vector3 for 2D, use Vector2. If you do things like Distance on a Vector3 and you have anything in the Z then it counts that as part of the distance. This can cause subtle bugs which drive you crazy finding them.
     
    Zagreus0 likes this.
  7. Zagreus0

    Zagreus0

    Joined:
    Jan 22, 2023
    Posts:
    5
    thank you! this was very helpful
     
    MelvMay likes this.