- Home /
,How do i make a D-Pad that functions on AddForce??
I am having trouble making a on-screen D-Pad that functions on AddForce. I have looked up other tutorials but they work on Axis. The reason i need AddForce for my game is because i am making a 3D mobile game where you roll a sphere to the finish. If i were to use axis, the ball wouldnt roll, it would just go forward which is just ugly. I need the D-Pad to add force to the player in the direction of the joystick. The ball already rolls on WASD, heres the code for it:
using UnityEngine;
public class PlayerMovement : MonoBehaviour{
public Rigidbody rb;
public float forwardForce = 2000f; public float sidewaysForce = 500f;
// Update is called once per frame void FixedUpdate() {
if ( Input.GetKey("d") )
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0);
}
if (Input.GetKey("w") )
{
rb.AddForce(0, 0, sidewaysForce * Time.deltaTime);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, 0, -sidewaysForce * Time.deltaTime);
}
} }
Any help is appreciated, even some code wouldn't hurt as i'm stuck on this for 2 weeks. Thank you very much in advance.
Your answer
Follow this Question
Related Questions
Touch input position determining player movement speed. 0 Answers
Simple D-Pad (Joystick) controls (Android) 2 Answers
How to trigger sound only when both fingers lifted off screen 1 Answer
Move a rigidbody smoothly by swipe 3 Answers
,Look rotation viewing vector is zero and character always facing 1 direction when idle 0 Answers