- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
besieger1 · Nov 25, 2013 at 05:04 PM ·
controllerjoystickshooter
GamePad joystick
Hey guys i have been trying to create a dual stick shooter with the xbox controller or any for that matter.
Now I have got movement working fine but rotation doesn't work how I would like, when turning using the right joystick it seems to snap to certain angles like from 90 to 0 to 45 etc.
I have done some googling and found most people have the same problem...
using UnityEngine;
using System.Collections;
public class DualStickShooter : MonoBehaviour {
public float PlayerRotationSpeed = 1.0f;
public float PlayerSpeed = 100.0f;
public Transform bullet;
public GameObject orginalBullet;
// Update is called once per frame
void Update () {
// This assigns a float to each joystic axis (left and right analogue stick)
// which is used as the input. This result is multiplied by the PlayerSpeed
// and real time.
float axisHorizontal = Input.GetAxis("1Horizontal") * PlayerSpeed * Time.deltaTime;
float axisVertical = Input.GetAxis("1Vertical") * PlayerSpeed * Time.deltaTime;
float axisHorizontal2 = Input.GetAxis("1FireHorizontal") * PlayerSpeed * Time.deltaTime;
float axisVertical2 = Input.GetAxis("1FireVertical") * PlayerSpeed * Time.deltaTime;
float translateX = axisHorizontal * PlayerSpeed * Time.deltaTime;
float translateY = axisVertical * PlayerSpeed * Time.deltaTime;
// move the player towards the joystick direction
transform.Translate(translateX, 0, translateY);
// rotate the player towards the joystick direction
transform.localRotation = Quaternion.Euler(0.0f,(Mathf.Atan2(axisHorizontal2, axisVertical2) * Mathf.Rad2Deg), 0.0f);
// shoot with left upper-bumper.
if (Input.GetKeyDown("joystick button 6"))
{
shootBullet();
}
}
void shootBullet()
{
Transform shot = Instantiate(bullet, transform.position, transform.rotation) as Transform;
Physics.IgnoreCollision(shot.collider, collider); //Ignore collision from bullet to shooter.
}
void OnCollisionEnter()
{
rigidbody.velocity = Vector3.zero;
}
void OnCollisionExit()
{
rigidbody.velocity = Vector3.zero;
}
void ShootDelay()
{
//canShoot = true;
}
}
Comment
Your answer