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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Difference between these 2 scripts to move an object

Discussion in '2D' started by warrenbrandt, Sep 13, 2018.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    hi guys

    been messing around moving an object

    i originally worked on this one

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class UfoControl : MonoBehaviour
    5. {
    6.     public float speed;
    7.     private Rigidbody2D rb;
    8.     private Vector2 moveVelocity;
    9.     void start ()
    10.     {
    11.         rb = GetComponent<Rigidbody2D>();
    12.     }
    13.     void update ()
    14.     {
    15.         Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    16.         moveVelocity = moveInput.normalized * speed;
    17.     }
    18.     void FixedUpdate ()
    19.     {
    20.         rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
    21.     }
    then worked on this one

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class ControlUfo : MonoBehaviour
    5. {
    6.  
    7.     public float speed;
    8.  
    9.     private Rigidbody2D rb;
    10.     private Vector2 moveVelocity;
    11.  
    12.     private float ufoSpeed;
    13.  
    14.     void Start ()
    15.     {
    16.         speed = 3;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         float axisX = Input.GetAxis("Horizontal");
    22.         float axisY = Input.GetAxis("Vertical");
    23.  
    24.         transform.Translate(new Vector3(axisX, axisY) * Time.deltaTime * speed);
    25.  
    26.     }
    27.  
    28.     void FixedUpdate()
    29.     {
    30.      
    31.     }
    32. }
    The second one is shorter,and seems to work better...the object moves smoother as well as stops much nicer when a key isnt pressed

    but it doesnt seem tobe using a rigid body to move,i have never seen this.

    what is the difference between the 2? will i have problems adding boundries etc etc with the second script?
    sorry,complete newb here!!!

    thanks for reading
     
  2. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    oh i see when i coded stuff it added the rigid body portion,is that right? does unity add stuff automatically?
     
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Using a Rigidbody to move an object will make it obey physics such as collisions and gravity, and also takes into account attributes like velocity and mass.

    Using the Transform component to move an object does not.
     
    vakabaka likes this.
  4. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    so its obviously better to use rigid body for a player object...transform would be for like an arrow pointing the player in the right direction etc

    oh and thanks for answering...appreciated