Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by unity_ArPSLL8ZtWg_nw · Oct 30, 2021 at 08:59 PM · scripting problemvelocitymovement script

GameObject does not move with RigidBody2D velocity.

I have a gameobject in the hierarchy called Player. It has one child, PlayerSprite, which is a 2D sprite square. I made a script to make the sprite, or rather the gameobject as a whole, move with input. Here is the script I made.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour {   
     [SerializeField] float jumpHeight;
     [SerializeField] float moveSpeed;
     void Update()
     {
         if       (Input.GetKeyDown(KeyCode.Space))   Jump();
         else if  (Input.GetKey(KeyCode.LeftArrow))   MoveLeft();
         else if  (Input.GetKey(KeyCode.RightArrow))  MoveRight();
     }
     void Jump() {
         float totalJump = jumpHeight * Time.deltaTime;
         GetComponent<Rigidbody2D>().velocity = new Vector3(0, totalJump, 0);
     }
     void MoveRight() {
         float totalMovement = moveSpeed * Time.deltaTime;
         GetComponent<Rigidbody2D>().velocity = new Vector3(totalMovement, 0, 0);
     }
     void MoveLeft() {
         float totalMovement = - moveSpeed * Time.deltaTime;
         GetComponent<Rigidbody2D>().velocity = new Vector3(totalMovement, 0, 0);
     }
 }
 

The script is attached to the Player gameobject. But somehow I just can't get it to work. It does not move. I have spent nearly an hour trying to figuring this out, but I don't know what to do. Can someone help and tell me what has gone wrong?

Edit: One interesting to note is that when I press left or right arrow keys, the x position of the gameobject in transform changes, but its position in the scene view does not change. Very weird.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image logicandchaos · Oct 30, 2021 at 11:48 PM 0
Share

Do you have a rigidbody2d attached to your gameObject? What is your moveSpeed set to in the inspector? Is the camera following the player? Are you getting any errors?

avatar image unity_ArPSLL8ZtWg_nw logicandchaos · Oct 31, 2021 at 05:25 AM 0
Share
  1. Yes, a RigidBody2D is attached to the gameobject.

  2. Movespeed is more than 0, around 10-20

  3. No, the camera is fixed at a place.

  4. There are no errors in the console.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by B0shy · Oct 31, 2021 at 11:54 AM

 GetComponent<Rigidbody2D>().velocity = new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed, GetComponent<Rigidbody2D>().velocity.y, 0);

Put this code into void FixedUpdate()


And make Jump()

 GetComponent<Rigidbody2D>().velocity = new Vector3(GetComponent<Rigidbody2D>().velocity.x, jumpHeight, 0);

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unity_ArPSLL8ZtWg_nw · Oct 31, 2021 at 12:57 PM 0
Share

Thanks! It works. But what exactly caused the previous code to not work?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

236 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i maintain the same speed in the air? 1 Answer

Move character with AddForce 2 Answers

Character Animator controller/movement 1 Answer

[ C#] Character Movement based on Camera Direction. 2 Answers

What should i write that when i press "Shift" it will speed up idk how shift is named? Here's the code: 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges