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.

Why the position of the player1 go to 0 0 0?

Discussion in 'Scripting' started by Ikerpini, Jul 3, 2022.

  1. Ikerpini

    Ikerpini

    Joined:
    Aug 19, 2021
    Posts:
    22
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Players : MonoBehaviour
    7. {
    8.     public bool player1;
    9.     public float speed = 8;
    10.     public Rigidbody2D rb;
    11.  
    12.     private float move;
    13.     private Vector2 startPos;
    14.  
    15.  
    16.  
    17.     void Start()
    18.     {
    19.  
    20.         startPos = transform.position;
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.  
    26.         if (player1)
    27.         {
    28.  
    29.             Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    30.             mousePosition.x = Camera.main.transform.position.x;
    31.             transform.position = mousePosition;
    32.  
    33.  
    34.         }
    35.         else
    36.         {
    37.             move = Input.GetAxisRaw("Vertical");
    38.  
    39.         }
    40.  
    41.         rb.velocity = new Vector2(rb.velocity.x, move * speed);
    42.  
    43.  
    44.  
    45.  
    46.     }
    47. }
     
  2. venom789

    venom789

    Joined:
    Apr 18, 2022
    Posts:
    178
    private Vector2 startPos;
    is vector 2 no vector 3.


    I don't know if you know this but you can do this to change the value of a vector variable :

    private Vector2 startPos = new Vector2(x, y); 


    You replace x and y with the numbers of your choice.
     
    Ikerpini likes this.
unityunity