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 apply to two vectors the same variation ?

Discussion in 'Scripting' started by esiorbmA, Dec 4, 2020.

  1. esiorbmA

    esiorbmA

    Joined:
    Dec 1, 2020
    Posts:
    9
    Hello,

    How to apply to a Vector3 the same variation of another Vector3 ?

    Here, I want to subtract from myPoint the movement of the camera.


    Code (CSharp):
    1. Vector3 camPosition;
    2. Vector3 myPoint;
    3.  
    4.  
    5. Camera cam;
    6.  
    7. void Start()
    8. {
    9. cam = Camera.main;
    10. camPosition = cam.transform.position;
    11. }
    12.  
    13. void Update()
    14. {
    15. myPoint = cam.ScreenToWorldPoint(Input.mousePosition);
    16. }
    17.  

    I tried
    Code (CSharp):
    1. myPoint = cam.ScreenToWorldPoint(Input.mousePosition) -  camPosition ;
    but it creates a terrible offset.

    So what I try to do is to measure the variation of the x, y, z of the camera's position and substract it to myPoint, but without its initial values.





    in other terms, lets suppose that my camera position is initialy: camPos = (150,-30,0)
    and my point position is myPoint = (0,0,0)

    then camera moves to theses coordinates: camPos = (180,10,0)
    so a variation of (+30,+40,0)

    how to make myPoint = (30,40,0) ?
    and how to do this for every movement of the camera?

    Thanks in advance for your help :)
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @esiorbmA

    You didn't even mention what exactly you are doing, TBH I have no idea what you are doing.

    "but it creates a terrible offset."

    Camera ScreenToWorldPoint won't work the same way for Perspective and Orthographic camera. Which one you are using?
     
    Bunny83 likes this.
  3. esiorbmA

    esiorbmA

    Joined:
    Dec 1, 2020
    Posts:
    9
    my camera is orthographic

    In my game, the camera follows the player. When the camera moves, I want to substract this movement to the Vector3 myPoint so it is not affected by the camera movement.



    In more details, my problem is this

    endPoint moves with the camera even if the cursor doesn't on screen.
    I thought
    Code (CSharp):
    1. endPoint =  cam.ScreenToWorldPoint(Input.mousePosition) - HERE MOVEMENT OF THE CAMERA ;
    would work, but I don't know how to do it

    the full code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DG.Tweening;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     #region Singleton class: GameManager
    9.  
    10.     public static GameManager instance;
    11.  
    12.     void Awake()
    13.     {
    14.         if (instance == null)
    15.         {
    16.             instance = this;
    17.         }
    18.     }
    19.  
    20.     #endregion
    21.  
    22.     Camera cam;
    23.  
    24.  
    25.     public Ball ball;
    26.  
    27.     public Trajectory trajectory;
    28.  
    29.     [SerializeField] float pushForce = 3f;
    30.  
    31.     public bool isDragging = false;
    32.  
    33.     /*public Vector2 startPoint;
    34.     public Vector2 endPoint;*/
    35.  
    36.  
    37.     Vector2 startPoint;
    38.  
    39.     Vector2 endPoint;
    40.  
    41.     Vector2 direction;
    42.     Vector2 force;
    43.     [Range(0, 5)] public float distance;
    44.  
    45.  
    46.     void Start()
    47.     {
    48.         cam = Camera.main;
    49.         ball.DesactivateRb();
    50.     }
    51.  
    52.     void Update()
    53.     {
    54.         ball.CheckIsMoving();
    55.  
    56.         ball.CheckIsGrounded();
    57.  
    58.  
    59.         if ((Input.GetMouseButtonDown(0)) && (ball.isMoving == false) && (ball.isGrounded == true))
    60.         {
    61.             isDragging = true;
    62.             OnDragStart();
    63.         }
    64.  
    65.         if ((Input.GetMouseButtonUp(0)) && (isDragging == true))
    66.         {
    67.             isDragging = false;
    68.             ball.rb.constraints = RigidbodyConstraints2D.None;
    69.  
    70.             OnDragEnd();
    71.         }
    72.  
    73.  
    74.         if (isDragging)
    75.         {
    76.             OnDrag();
    77.         }
    78.     }
    79.  
    80.     private void FixedUpdate()
    81.     {
    82.  
    83.         if (ball.isSticked == true)
    84.         {
    85.  
    86.             ball.DesactivateRb();
    87.             //ball.rb.constraints = RigidbodyConstraints2D.FreezePositionY | RigidbodyConstraints2D.FreezePositionX | RigidbodyConstraints2D.FreezeRotation;
    88.        
    89.             ball.isSticked = false;
    90.         }
    91.     }
    92.  
    93.  
    94.  
    95.     //---------------------------- Drag
    96.  
    97.     void OnDragStart()
    98.     {
    99.         ball.DesactivateRb();
    100.  
    101.         startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
    102.     /*    startPoint = cam.ScreenPointToRay(Input.mousePosition);
    103.         startPoint1 = startPoint.origin;
    104.  
    105.         Debug.DrawRay(startPoint.origin, startPoint.direction * 10, Color.red);*/
    106.         trajectory.Show();
    107.     }
    108.  
    109.     void OnDrag()
    110.     {
    111.         endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
    112.     /*    endPoint = cam.ScreenPointToRay(Input.mousePosition);
    113.  
    114.         endPoint1 = endPoint.origin;*/
    115.    
    116.         //Debug.DrawRay(endPoint.origin, endPoint.direction * 10, Color.yellow);
    117.    
    118.         // on limite la distance maximum à 6.5f
    119.         if ((Vector2.Distance(startPoint, endPoint)) < 6.5f)
    120.         {
    121.             distance = (Vector2.Distance(startPoint, endPoint));
    122.         }
    123.  
    124.  
    125.         direction = (startPoint - endPoint).normalized;
    126.         force = (direction * distance * pushForce);
    127.  
    128.  
    129.  
    130.         //just for Debug
    131.         Debug.DrawLine(startPoint, endPoint);
    132.  
    133.         trajectory.UpdateDots(ball.pos, force);
    134.  
    135.     }
    136.     void OnDragEnd()
    137.     {
    138.         distance = 0;
    139.         //push the ball
    140.         ball.ActivateRb();
    141.  
    142.         ball.Push(force);
    143.  
    144.         trajectory.Hide();
    145.     }
    146.  
    147.  
    148.  
    149. }
    150.  
     
    Last edited: Dec 4, 2020
  4. esiorbmA

    esiorbmA

    Joined:
    Dec 1, 2020
    Posts:
    9
    please can anyone help me ?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    For interesting camera stuff, probably best to look into Cinemachine (package from Unity).

    If you want to debug "wrong data" bugs such as above, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.
     
  6. dhumildholiya97

    dhumildholiya97

    Joined:
    Sep 1, 2020
    Posts:
    2
    • I got this same problem recently and above discussion helped understand my problem very well. And Now I got solution.
    • Main problem is that ``Start Point`` and ``End Point`` are in world position. So When camera moves and it Recalculates `End Point` and changes the World position which affects the projectile motion.
    • Solution is to convert both `Start Point` and `End Point` to local position of camera so when `End Point` get recalculates its still in the reference of Camera's position and does not affect the projectile motion.
    • Code (CSharp):
      1. if (Input.GetMouseButtonDown(0))
      2.                     {
      3.                         _startMousePosition = _cam.ScreenToWorldPoint(Input.mousePosition);
      4.                         _startMousePosition = _cam.transform.InverseTransformPoint(_startMousePosition);
      5.                         _projectileIndicator.PointsSetActive(true, transform.position);
      6.                     }
      7.  
      8.                     if (Input.GetMouseButton(0))
      9.                     {
      10.                         Vector2 position = _cam.ScreenToWorldPoint(Input.mousePosition);
      11.                         position = _cam.transform.InverseTransformPoint(position);
      12.                         _initialVelocity = (_startMousePosition - position) * speed;
      13.                         _projectileIndicator.DrawProjectileTrajectory(transform.position, _initialVelocity,
      14.                             Physics2D.gravity * _rb.gravityScale, timeInterval);
      15.                     }