Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Logan_ak · Mar 09, 2018 at 03:15 PM · rigidbody2dplayer movementrigidbody.velocity

I want to Keep player moving in up direction as well as to move sideways by the user input. ,How can i keep the player moving upwards and sideways as well?

I am making a 2d game in which player moves forword by default and it is supposed to turn sideways as per user input following is the code I have written

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class playermovement : MonoBehaviour {

 public float Sidespeed = 10f;
 public float UpSpeed = 5f;
 public float mapwidth = 5f;
 private Rigidbody2D rb;
 float movement = 0.0f;
 // Use this for initialization

 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame
 void Update()
 {
     
     movement = Input.GetAxis("Horizontal") * Time.deltaTime * Sidespeed;
     Vector2 newpos = rb.position + Vector2.right * movement;
     newpos.x = Mathf.Clamp(newpos.x, -mapwidth, mapwidth);
     
     rb.MovePosition(newpos);
     
 }

 void FixedUpdate()
 {
     Vector2 velocity = rb.velocity;
     velocity.y = UpSpeed;
     rb.velocity = velocity;

 }

},I am trying to make a 2d game in which player is continuously moving upwards and I want him to move Side to side with the userinput. Following is the code I have written.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class playermovement : MonoBehaviour {

 public float Sidespeed = 10f;
 public float UpSpeed = 5f;
 public float mapwidth = 5f;
 private Rigidbody2D rb;
 float movement = 0.0f;
 // Use this for initialization

 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame
 void Update()
 {
     
     movement = Input.GetAxis("Horizontal") * Time.deltaTime * Sidespeed;
     Vector2 newpos = rb.position + Vector2.right * movement;
     newpos.x = Mathf.Clamp(newpos.x, -mapwidth, mapwidth);
     
     rb.MovePosition(newpos);
     
 }

 void FixedUpdate()
 {
     Vector2 velocity = rb.velocity;
     velocity.y = UpSpeed;
     rb.velocity = velocity;

 }


}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by raincrowgames · Mar 09, 2018 at 04:05 PM

Is there a specific reason you are using rigid body? you simply to transform.Translate(0,speed,0) to move forward and then you can do transform.Translate(Input.GetAxis("Horizontal")*speed,0,0). That should work unless I am missing something.

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 Logan_ak · Mar 11, 2018 at 03:30 PM 0
Share

Also can you tell me how can I clamp the position between two points on horizontal. Btw your Info helped

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

76 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

Related Questions

Why won't my RigidBody 2D stop moving?, 0 Answers

rigid.velocity.x = 0 for unknow reason 0 Answers

Trying to get objects to instantiate and move at different angles 1 Answer

2D Rigidbody Velocity Being Weird 1 Answer

How can I add acceleration and deceleration to my Rigidbody 2D? 0 Answers


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