- Home /
Rigidbody only moving in one direction
So I'm not a coder so this is hell for me, but I am trying to get my head around this so bear with me please.
I got this code
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
public static float distanceTraveled;
private Touch curTouch;
public float speed;
public float maxSpeed;
public float maxSpeedConstant;
//Virtual buttons left and right half of the screen
private Rect leftHalf = new Rect(0F,0F,Screen.width/2,Screen.height);
private Rect rightHalf = new Rect(Screen.width/2,0F,Screen.width/2,Screen.height);
public void Update() {
distanceTraveled = transform.localPosition.x;
}
public void FixedUpdate() {
Movement ();
}
public void Movement(){
if(rigidbody.velocity.magnitude > maxSpeed)
{
rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed;
}
//Accelerometer Control up/down
Vector3 dirAcc = Vector3.zero;
dirAcc.y = Input.acceleration.y*10;
rigidbody.AddForce(0.0F,0.0F,dirAcc.y);
if(Input.touchCount != 0){
if (leftHalf.Contains (curTouch.position)){
maxSpeed = maxSpeed*2;
rigidbody.AddForce(-speed,0,0);
}else{
if (rightHalf.Contains(curTouch.position)){
maxSpeed = maxSpeed*2;
rigidbody.AddForce(+speed,0,0);
}
}
}else{
if(Input.touchCount == 0){
maxSpeed = maxSpeedConstant;
rigidbody.AddForce(speed,0,0);
}
}
}
}
What I'm trying to achieve is that the play is able to control the up/down movement of the character via accelerometer and right/left movement by touching the right/left side of the screen.
With the above code the touch area does not matter the character will always move backwards and accelerometer input is entirely ignored. What bothers me is that the above code (accelerometer part only) worked with transform.Translate instead of rigidbody.AddForce. But from what I've read on the internet I'm going to need rigidbodies if I want collisions.
So any help or advice regarding code structure/syntax or solution towards my problem is appreciated.
Answer by ExpiredIndexCard · Jun 16, 2013 at 08:19 PM
If you want collisions, you do need a rigidbody but you do not have to make the movement based on physics. Use Translate only and add a rigidbody to the gameobject. Simple. That means that you can easily make your character move, and you will still have collisions.
Thanks I did not know this! Still, the problem of the player moving in only one direction whilst pressing the screen persist.
Are my Rectangles set up the right way?
private Rect leftHalf = new Rect(0F,0F,Screen.width/2,Screen.height);
private Rect rightHalf = new Rect(Screen.width/2,0F,Screen.width/2,Screen.height);
And is my Input check correct?
if(Input.touchCount != 0){
if (leftHalf.Contains (curTouch.position)){
transform.Translate(-Vector3.right*speed*Time.deltaTime);
}else{
if (rightHalf.Contains(curTouch.position)){
transform.Translate(Vector3.right*speed*Time.deltaTime);
}
}
}else{
if(Input.touchCount == 0){
transform.Translate(Vector3.right*speed*Time.deltaTime);
}
}
I have never worked with mobile. I'm sorry but I guess someone else has to answer thatfor you. Sorry
Answer by Wibber · Jun 18, 2013 at 12:16 PM
I feex. Instead of using Touch curTouch; to define the touch position I had to use a Vector2,use Input.GetTouch(0) for the if statement and now I set curTouch everytime a touch occurs.
Here is what the essential part looks like now :
public void Movement(){
//Accelerometer Control up/down
Vector3 dirAcc = Vector3.zero;
dirAcc.y = Input.acceleration.y*.5F;
transform.Translate(0F,0F,dirAcc.y);
if(Input.touchCount != 0){
Vector2 curTouch = Input.GetTouch(0).position;
if (leftHalf.Contains (curTouch)){
//transform.Translate(-Vector3.right*speed*Time.deltaTime);
transform.Translate (-moveVec*2*Time.deltaTime);
}else{
if (rightHalf.Contains(curTouch)){
//transform.Translate(Vector3.right*speed*Time.deltaTime);
transform.Translate (moveVec*2*Time.deltaTime);
}
}
}else{
if(Input.touchCount == 0){
//transform.Translate(Vector3.right*speed*Time.deltaTime);
transform.Translate (moveVec*Time.deltaTime);
}
}
}