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

I can't get my sprite using the rigidbody physics to move.

Discussion in 'Scripting' started by Maxfive0, Mar 22, 2018.

  1. Maxfive0

    Maxfive0

    Joined:
    Mar 16, 2018
    Posts:
    8
    I have a rigidbody2d on but my still doesn't move or rotate.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public abstract class SpaceCharacter : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     private float speed;
    10.     [SerializeField]
    11.     private float turnSpeed;
    12.  
    13.     protected Vector3 vertical;
    14.     protected Vector3 horizontal;
    15.  
    16.     private Rigidbody2D myRigidbody;
    17.  
    18.     // Use this for initialization
    19.     protected virtual void Start()
    20.     {
    21.         myRigidbody = GetComponent<Rigidbody2D>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     protected virtual void Update()
    26.     {
    27.  
    28.     }
    29.  
    30.     private void FixedUpdate()
    31.     {
    32.         Move();
    33.     }
    34.  
    35.     public void Move()
    36.     {
    37.         myRigidbody.velocity = vertical.normalized * speed;
    38.         myRigidbody.velocity = horizontal.normalized * turnSpeed;
    39.     }
    40. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpacePlayer : SpaceCharacter
    6. {
    7.     [SerializeField]
    8.     private SpaceStat health;
    9.  
    10.     [SerializeField]
    11.     private SpaceStat mana;
    12.  
    13.     private float initHealth = 100;
    14.  
    15.     private float initMana = 50;
    16.  
    17.     protected override void Start()
    18.     {
    19.  
    20.         health.Initialize(initHealth, initHealth);
    21.         mana.Initialize(initMana, initMana);
    22.  
    23.         base.Start();
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     protected override void Update()
    28.     {
    29.         GetInput();
    30.         base.Update();
    31.     }
    32.  
    33.     private void GetInput()
    34.     {
    35.         vertical = Vector3.zero;
    36.         horizontal = Vector3.zero;
    37.  
    38.         ///This Is Used For Debugging Only
    39.         ///
    40.         if (Input.GetKeyDown(KeyCode.I))
    41.         {
    42.             health.MyCurrentValue -= 10;
    43.             mana.MyCurrentValue -= 10;
    44.         }
    45.         if (Input.GetKeyDown(KeyCode.O))
    46.         {
    47.             health.MyCurrentValue += 10;
    48.             mana.MyCurrentValue += 10;
    49.         }
    50.  
    51.  
    52.         if (Input.GetKey(KeyCode.W))
    53.         {
    54.             vertical += Vector3.up;
    55.         }
    56.         if (Input.GetKey(KeyCode.A))
    57.         {
    58.             horizontal += Vector3.back;
    59.         }
    60.         if (Input.GetKey(KeyCode.S))
    61.         {
    62.             vertical += Vector3.down;
    63.         }
    64.         if (Input.GetKey(KeyCode.D))
    65.         {
    66.             horizontal += Vector3.forward;
    67.         }
    68.     }
    69. }
     
  2. Maxfive0

    Maxfive0

    Joined:
    Mar 16, 2018
    Posts:
    8
    The problem is I can't move or rotate my 2dplayer.
     
  3. Pagefile

    Pagefile

    Joined:
    Feb 10, 2013
    Posts:
    51
    What happens if you make FixedUpdate() virtual in the base class and override it in the child class and call base.FixedUpdate()?
     
  4. Maxfive0

    Maxfive0

    Joined:
    Mar 16, 2018
    Posts:
    8
    Like this?It doesn't work.
    Code (CSharp):
    1. private virtual void FixedUpdate()
    2.     {
    3.         Move();
    4.     }
     
  5. Maxfive0

    Maxfive0

    Joined:
    Mar 16, 2018
    Posts:
    8
    I don't know how to fix the problem.The game works I just can't move the player.
     
  6. Maxfive0

    Maxfive0

    Joined:
    Mar 16, 2018
    Posts:
    8
    this was my original script.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public abstract class SpaceCharacter : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     private float speed;
    10.     [SerializeField]
    11.     private float turnSpeed;
    12.  
    13.     protected Vector3 vertical;
    14.     protected Vector3 horizontal;
    15.  
    16.     // Use this for initialization
    17.     protected virtual void Start()
    18.     {
    19.  
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     protected virtual void Update()
    24.     {
    25.         Move();
    26.     }
    27.     public void Move()
    28.     {
    29.         transform.Translate(vertical * Time.deltaTime * speed);
    30.         transform.Rotate(horizontal * Time.deltaTime * turnSpeed);
    31.     }
    32. }
    33.  
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, this piece of code is setting the velocity twice (no rotation is included):
    (the second one means the first is ignored, of course)
    Code (csharp):
    1.  
    2. public void Move()
    3.     {
    4.         myRigidbody.velocity = vertical.normalized * speed;
    5.         myRigidbody.velocity = horizontal.normalized * turnSpeed;
    6.     }
    Did you want angularVelocity for one? Note, you could also simply set the rotation, too, or use the transform.Rotate -- just depends what you want.

    Okay, I just re-read the code. You're checking for Input in Update; now, for some of the 'KeyDown' checks, that's good, but for the 'GetKey' it's not good with your current code.

    You are setting the vertical and horizontal to zero at the beginning of each update, so it will be unreliable to read in FixedUpdate. You can safely read 'GetKey' inside fixed update (just not KeyDown/Up, or any other '1 frame' event).
     
    Pagefile likes this.