Switch PC Controls to Android ?
Hello I have a script that work perfectly on PC platforms
The character move to the left or right with A and D key But i'm trying to make it work on Android to. I tried Accelerometer but it doesn't work as intended so I end up adding 2 button Left and Right and the player will have to touch it to turn the character left or right
Here is my code :
using UnityEngine;
public class PlayerController : MonoBehaviour {
public float moveSpeed = 10f;
public float rotationSpeed = 10f;
private float rotation;
private Rigidbody rb;
void Start ()
{
rb = GetComponent<Rigidbody>();
}
void Update ()
{
rotation = Input.GetAxisRaw("Horizontal");
}
void FixedUpdate ()
{
rb.MovePosition(rb.position + transform.forward * moveSpeed * Time.fixedDeltaTime);
Vector3 yRotation = Vector3.up * rotation * rotationSpeed * Time.fixedDeltaTime;
Quaternion deltaRotation = Quaternion.Euler(yRotation);
Quaternion targetRotation = rb.rotation * deltaRotation;
rb.MoveRotation(Quaternion.Slerp(rb.rotation, targetRotation, 50f * Time.deltaTime));
//transform.Rotate(0f, rotation * rotationSpeed * Time.fixedDeltaTime, 0f, Space.Self);
}
}
Thank you in Advance
I also use a SmoothFollow on the Camera if it can help anyone
using UnityEngine;
public class SmoothFollow : MonoBehaviour {
public Transform target;
public float smoothness = 1f;
public float rotationSmoothness = .1f;
public Vector3 offset;
private Vector3 velocity = Vector3.zero;
// Update is called once per frame
void FixedUpdate () {
if (target == null)
{
return;
}
Vector3 newPos = target.TransformDirection(offset);
//transform.position = newPos;
transform.position = Vector3.SmoothDamp(transform.position, newPos, ref velocity, smoothness);
Quaternion targetRot = Quaternion.LookRotation(-transform.position.normalized, target.up);
//transform.rotation = targetRot;
transform.rotation = Quaternion.Lerp(transform.rotation, targetRot, Time.deltaTime * rotationSmoothness);
}
}
Answer by Palass64 · Aug 17, 2017 at 10:13 AM
Forgot to mention that i have a sphere with a cube on it attracted to the sphere and they both move horizontaly I use an Attractor:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class FauxGravityAttractor : MonoBehaviour {
public static FauxGravityAttractor instance;
private SphereCollider col;
void Awake ()
{
instance = this;
col = GetComponent<SphereCollider>();
}
public float gravity = -10f;
public void Attract (Rigidbody body)
{
Vector3 gravityUp = (body.position - transform.position).normalized;
body.AddForce(gravityUp * gravity);
RotateBody(body);
}
public void PlaceOnSurface (Rigidbody body)
{
body.MovePosition((body.position - transform.position).normalized * (transform.localScale.x * col.radius));
RotateBody(body);
}
void RotateBody (Rigidbody body)
{
Vector3 gravityUp = (body.position - transform.position).normalized;
Quaternion targetRotation = Quaternion.FromToRotation(body.transform.up, gravityUp) * body.rotation;
body.MoveRotation (Quaternion.Slerp(body.rotation, targetRotation, 50f * Time.deltaTime));
}
}
And a gravity body for the cube
using UnityEngine;
[RequireComponent(typeof(Rigidbody))] public class FauxGravityBody : MonoBehaviour {
private FauxGravityAttractor attractor;
private Rigidbody rb;
public bool placeOnSurface = false;
void Start ()
{
rb = GetComponent<Rigidbody>();
attractor = FauxGravityAttractor.instance;
}
void FixedUpdate ()
{
if (placeOnSurface)
attractor.PlaceOnSurface(rb);
else
attractor.Attract(rb);
}
}
I just want to Replace the A D button to some touch on phone like Left and Right with a button or touch the screen left and right to turn my cube on the sphere
Thanks
Your answer
Follow this Question
Related Questions
Dpad Controls highlighted buttons 0 Answers
Buttons for Player movement 0 Answers
[Android] Character movement script help 1 Answer