Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Rigidbody2D AddForce towards object

$
0
0
So I have a rigidbody2D component attached to my player. I have the player attach and detach to these rails I am trying to build. While on the rails I have the controls switch from the default WASD controls (which work as I want them to) to W and S for going up and down those rails to and from two nodes. Attaching to and from the rails work fine. However, the player only seems to want to move vertically no matter where I place the other nodes. Put simply, I want the player to move towards and away from the node when pressing W and S respectively. Here's the code for movement while on the rails (again, attaching to the rail and switching control configuration works as I want it to.) //railroad movement else if (Railroad == true) { dir = Node2.transform.position - transform.position; dir = dir.normalized; if (Input.GetKey (KeyCode.W)) { rigidbody2D.AddForce (dir * movement); } if (Input.GetKey (KeyCode.S)) { rigidbody2D.AddForce (dir * -movement); } } So what am I doing wrong?

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>