- Home /
Question by
sachinbirajdar · Jun 23, 2018 at 12:22 PM ·
rotationrotatearound
Rotate player using touch pad.
Hello, I am using dual touch controls pad in my game.I have a player which I want to rotate in only x and y axis.But when I play it on phone, it rotates in all 3 axis(x,y,z) which I don't want to happen.Here is the script attached to my player.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour {
float turnaroundx;
float turnaroundy;
float rotSpeed = 20;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
turnaroundx = CrossPlatformInputManager.GetAxis ("Mouse X")*rotSpeed*Time.deltaTime;
turnaroundy = CrossPlatformInputManager.GetAxis ("Mouse Y")*rotSpeed*Time.deltaTime;
transform.Rotate(Vector3.up, -turnaroundx );
transform.Rotate(Vector3.right, turnaroundy );
}
}
Comment
Answer by Tawakal125 · Dec 30, 2019 at 11:32 AM
use this code.....
public Rigidbody rb; public float speed;
void Start() { rb = GetComponent(); }
void FixedUpdate() {
float movehorizontal = CrossPlatformInputManager.GetAxis("Horizontal");
float movevertical = CrossPlatformInputManager.GetAxis("Vertical");
Vector3 movement = new Vector3(movehorizontal , 0.0f , movevertical);
rb.velocity = movement * speed;
}