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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

why is this script not moving my ufo?

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

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    following a tutorial to move a ufo sprite
    been at it for two hours no joy!!!

    does this script look alright?
    also i have the script attached to my ufo sprite...just nothing

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

    hamberge

    Joined:
    Aug 30, 2015
    Posts:
    36
    Is your speed value set to 0 in the inspector?
     
  3. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    no its 10 does the script look good?
     
  4. hamberge

    hamberge

    Joined:
    Aug 30, 2015
    Posts:
    36
    Are you getting any errors or warnings?
     
  5. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    no errors or warnings

    i had it working with a previous script...moved that script to another directory
    and was working on a script from another tutorial (which i couldnt get going)
    then i reapplied my working script...nothing
    even created a new scene setup sprite from scratch attached my script (original one that was working) what i posted was the newer script i couldnt get working
    nothing...

    have checked speed is on 10 (that always caught me out,used to checking this now)
    i dont know,its strange
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Start Update
     
    MisterSkitz likes this.
  7. hamberge

    hamberge

    Joined:
    Aug 30, 2015
    Posts:
    36
    Genius.
     
  8. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    sorry not really with you...can you please elaborate
     
  9. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    Sorry,
    write Start and Update with big letters at beginn.
     
  10. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    You are a genius my man thanks!!! such a stupid mistake i know c# java are case sensitive dammit!!!

    is there a way to stop it quicker when you take your finger off the key as its going on a bit before stopping

    hey thanks again
     
  11. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Have you tried adjusting your speed to a slower setting? Possibly reduce the drag in your inspector on you player's rigidbody physics component. Use more gravity? Less mass, more mass? Best way to learn is to play around. Don't be afraid of breaking it lol

    and don't worry about making mistakes. That's going to happen until you learn the syntax.
     
  12. roykoma

    roykoma

    Joined:
    Dec 9, 2016
    Posts:
    176
    This is probably caused by your usage of
    Input.GetAxis

    GetAxis gets smoothed, and therefore takes a while to get to 1/-1 or 0 again.
    If you want your Inputs to be used "directly" and just switch between 1, -1 and 0 immediately use GetAxisRaw
     
    vakabaka and MisterSkitz like this.
  13. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    hey thanks guys...after rewriting the script the ufo is moving like it should
    i even have the barriers setup so it cant fly off the screen

    you guys are great really thanks appreciate it a lot!!!
     
    MisterSkitz likes this.