Moving a 3d block on a surface left and right by touch button or image instead of A - D Keys - UNITY and C#
I've made a little game with the help of youtube tutorials and I have a problem.
Everything is ok with the game on pc, I use A and D to move left or right a cube to avoid obstacles in front.
The Block has a forwardspeed to move by itself forward with a given speed.
But.... and here comes the part where I am useless :))
I want to test my game on my Android Phone so I have to control the object with touchscreen Buttons or touching the left or right part of the mobile screen.
With A or D keys it's easy , but with mobile touch ... not so.
Down below is the code I use now for moving and a picture.
Thank you , any ideea is perfect.
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
c# unity3d unityscript
add a comment |
I've made a little game with the help of youtube tutorials and I have a problem.
Everything is ok with the game on pc, I use A and D to move left or right a cube to avoid obstacles in front.
The Block has a forwardspeed to move by itself forward with a given speed.
But.... and here comes the part where I am useless :))
I want to test my game on my Android Phone so I have to control the object with touchscreen Buttons or touching the left or right part of the mobile screen.
With A or D keys it's easy , but with mobile touch ... not so.
Down below is the code I use now for moving and a picture.
Thank you , any ideea is perfect.
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
c# unity3d unityscript
add a comment |
I've made a little game with the help of youtube tutorials and I have a problem.
Everything is ok with the game on pc, I use A and D to move left or right a cube to avoid obstacles in front.
The Block has a forwardspeed to move by itself forward with a given speed.
But.... and here comes the part where I am useless :))
I want to test my game on my Android Phone so I have to control the object with touchscreen Buttons or touching the left or right part of the mobile screen.
With A or D keys it's easy , but with mobile touch ... not so.
Down below is the code I use now for moving and a picture.
Thank you , any ideea is perfect.
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
c# unity3d unityscript
I've made a little game with the help of youtube tutorials and I have a problem.
Everything is ok with the game on pc, I use A and D to move left or right a cube to avoid obstacles in front.
The Block has a forwardspeed to move by itself forward with a given speed.
But.... and here comes the part where I am useless :))
I want to test my game on my Android Phone so I have to control the object with touchscreen Buttons or touching the left or right part of the mobile screen.
With A or D keys it's easy , but with mobile touch ... not so.
Down below is the code I use now for moving and a picture.
Thank you , any ideea is perfect.
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
// This is a reference to the Rigidbody component called "rb"
public Rigidbody rb;
public float forwardForce = 2000f; // Variable that determines the forward force
public float sidewaysForce = 500f; // Variable that determines the sideways force
// We marked this as "Fixed"Update because we
// are using it to mess with physics.
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
{
// Add a force to the right
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a")) // If the player is pressing the "a" key
{
// Add a force to the left
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y < -1f)
{
FindObjectOfType<GameManager>().EndGame();
}
}
}
c# unity3d unityscript
c# unity3d unityscript
asked Nov 23 '18 at 9:12
Cristian MergeaCristian Mergea
348
348
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Its not hard at all.
Start by extracting the movement into seperate methods
public void MoveRight()
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
public void MoveLeft()
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
MoveRight();
if (Input.GetKey("a")) // If the player is pressing the "a" key
MoveLeft();
}
All that's left to do is bind your buttons to those methods in the editor
Oh, Thank you. I will try right now.
– Cristian Mergea
Nov 23 '18 at 9:24
It's perfect, a few tweeks and works perfect. Thank you.
– Cristian Mergea
Nov 23 '18 at 9:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443618%2fmoving-a-3d-block-on-a-surface-left-and-right-by-touch-button-or-image-instead-o%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Its not hard at all.
Start by extracting the movement into seperate methods
public void MoveRight()
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
public void MoveLeft()
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
MoveRight();
if (Input.GetKey("a")) // If the player is pressing the "a" key
MoveLeft();
}
All that's left to do is bind your buttons to those methods in the editor
Oh, Thank you. I will try right now.
– Cristian Mergea
Nov 23 '18 at 9:24
It's perfect, a few tweeks and works perfect. Thank you.
– Cristian Mergea
Nov 23 '18 at 9:35
add a comment |
Its not hard at all.
Start by extracting the movement into seperate methods
public void MoveRight()
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
public void MoveLeft()
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
MoveRight();
if (Input.GetKey("a")) // If the player is pressing the "a" key
MoveLeft();
}
All that's left to do is bind your buttons to those methods in the editor
Oh, Thank you. I will try right now.
– Cristian Mergea
Nov 23 '18 at 9:24
It's perfect, a few tweeks and works perfect. Thank you.
– Cristian Mergea
Nov 23 '18 at 9:35
add a comment |
Its not hard at all.
Start by extracting the movement into seperate methods
public void MoveRight()
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
public void MoveLeft()
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
MoveRight();
if (Input.GetKey("a")) // If the player is pressing the "a" key
MoveLeft();
}
All that's left to do is bind your buttons to those methods in the editor
Its not hard at all.
Start by extracting the movement into seperate methods
public void MoveRight()
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
public void MoveLeft()
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
void FixedUpdate ()
{
// Add a forward force
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d")) // If the player is pressing the "d" key
MoveRight();
if (Input.GetKey("a")) // If the player is pressing the "a" key
MoveLeft();
}
All that's left to do is bind your buttons to those methods in the editor
answered Nov 23 '18 at 9:23
zambarizambari
2,2331316
2,2331316
Oh, Thank you. I will try right now.
– Cristian Mergea
Nov 23 '18 at 9:24
It's perfect, a few tweeks and works perfect. Thank you.
– Cristian Mergea
Nov 23 '18 at 9:35
add a comment |
Oh, Thank you. I will try right now.
– Cristian Mergea
Nov 23 '18 at 9:24
It's perfect, a few tweeks and works perfect. Thank you.
– Cristian Mergea
Nov 23 '18 at 9:35
Oh, Thank you. I will try right now.
– Cristian Mergea
Nov 23 '18 at 9:24
Oh, Thank you. I will try right now.
– Cristian Mergea
Nov 23 '18 at 9:24
It's perfect, a few tweeks and works perfect. Thank you.
– Cristian Mergea
Nov 23 '18 at 9:35
It's perfect, a few tweeks and works perfect. Thank you.
– Cristian Mergea
Nov 23 '18 at 9:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443618%2fmoving-a-3d-block-on-a-surface-left-and-right-by-touch-button-or-image-instead-o%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown