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.