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 set changing direction and constant speed for instantiated objects?

Discussion in 'Scripting' started by Kujji, Feb 20, 2023.

  1. Kujji

    Kujji

    Joined:
    Oct 20, 2021
    Posts:
    109
    I create meteorites around screen and adding them force, directed to the middle of screen (x = 0; y = 0), with devitation, but meteorites speed is different in the game, how can i make their speed the same.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Meteorite : MonoBehaviour
    6. {
    7.     [SerializeField] private float _speed = 6f;
    8.     [SerializeField] private float _directionDeviation = 4f;
    9.     private Rigidbody2D _rb;
    10.     private void Start()
    11.     {
    12.         _rb = GetComponent<Rigidbody2D>();
    13.         _rb.AddForce(-RandomDirection(this.transform) * _speed);
    14.     }
    15.  
    16.     private Vector2 RandomDirection(Transform transform)
    17.     {
    18.         float directionMultiplier = Random.Range(10 - _directionDeviation, 10 + _directionDeviation);
    19.         int coordinateToMultiply = Random.Range(0, 2);
    20.         if (coordinateToMultiply == 1)
    21.             return new Vector2(transform.position.x * directionMultiplier, transform.position.y);
    22.         else
    23.             return new Vector2(transform.position.x, transform.position.y * directionMultiplier);
    24.     }
    25. }
    26.  
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Normalize your direction. That will give it a length of 1.
    RandomDirection(this.transform).normalized * _speed
     
    Kujji likes this.
  3. Kujji

    Kujji

    Joined:
    Oct 20, 2021
    Posts:
    109
    Helped, thanks.
     
  4. Leuki

    Leuki

    Joined:
    Feb 27, 2014
    Posts:
    130
    Just as SF described above. The normalized property returns a vector with the same direction as the original vector, but with a length of 1. This means that the magnitude of the vector will be the same for all meteorites, which will result in the same speed.

    Code (CSharp):
    1. [SerializeField] private float _speed = 6f;
    2. [SerializeField] private float _directionDeviation = 4f;
    3.  
    4. private void Start()
    5. {
    6.     _rb = GetComponent<Rigidbody2D>();
    7.     Vector2 direction = RandomDirection(transform).normalized; // Normalize the direction
    8.     _rb.AddForce(-direction * _speed);
    9. }
    10.  
    11. private Vector2 RandomDirection(Transform transform)
    12. {
    13.     float directionMultiplier = Random.Range(10 - _directionDeviation, 10 + _directionDeviation);
    14.     int coordinateToMultiply = Random.Range(0, 2);
    15.     if (coordinateToMultiply == 1)
    16.         return new Vector2(transform.position.x * directionMultiplier, transform.position.y);
    17.     else
    18.         return new Vector2(transform.position.x, transform.position.y * directionMultiplier);
    19. }
    20.