Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Movement and position

Discussion in 'Getting Started' started by maciekmaster, Mar 21, 2019.

  1. maciekmaster

    maciekmaster

    Joined:
    Mar 21, 2019
    Posts:
    3
    2DProject
    I am beginner and i have two questions.
    1) I would like change position three objects (x,y,) in place "thisplace". How script should look like?

    2)How change script to have two scripts one with arrow and another one with WSDA.?
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. maciekmaster

    maciekmaster

    Joined:
    Mar 21, 2019
    Posts:
    3
    There are my scripts.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Ball : MonoBehaviour
    6. {
    7.  
    8.  
    9.     private void OnTriggerEnter2D(Collider2D collision)
    10.     {
    11.      
    12.         if (collision.tag == "Goal1")
    13.         {
    14.             if (collision.GetComponent<EnemyStats>() != null)
    15.             {
    16.                 PlayerStats.instance.AddPoints1(collision.GetComponent<EnemyStats>().HitPoints);
    17.             }
    18.        
    19.  
    20.  
    21.         }
    22.         if (collision.tag == "Goal2")
    23.         {
    24.             if (collision.GetComponent<EnemyStats>() != null)
    25.             {
    26.                 PlayerStats.instance.AddPoints2(collision.GetComponent<EnemyStats>().HitPoints);
    27.             }
    28.        
    29.             //this place
    30.         }
    31.  
    32.    
    33.  
    34.          
    35.       }
    36.  
    37. }
    38.  
    39.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement1 : MonoBehaviour
    6. {
    7.     public float speed;             //Floating point variable to store the player's movement speed.
    8.  
    9.     private Rigidbody2D rb2d;       //Store a reference to the Rigidbody2D component required to use 2D Physics.
    10.  
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         //Get and store a reference to the Rigidbody2D component so that we can access it.
    15.         rb2d = GetComponent<Rigidbody2D>();
    16.     }
    17.  
    18.     //FixedUpdate is called at a fixed interval and is independent of frame rate. Put physics code here.
    19.     void FixedUpdate()
    20.     {
    21.         //Store the current horizontal input in the float moveHorizontal.
    22.         float moveHorizontal = Input.GetAxis("Horizontal");
    23.  
    24.         //Store the current vertical input in the float moveVertical.
    25.         float moveVertical = Input.GetAxis("Vertical");
    26.  
    27.         //Use the two store floats to create a new Vector2 variable movement.
    28.         Vector2 movement = new Vector2(moveHorizontal, moveVertical);
    29.  
    30.         //Call the AddForce function of our Rigidbody2D rb2d supplying movement multiplied by speed to move our player.
    31.         rb2d.AddForce(movement * speed);
    32.     }
    33. }
    34.  
    35.  
    36.  
    37.