- Home /
Rotate object using analogue stick around player object
Ok, I've tried a bunch of things and nothing is ever remotely working. I was hoping one of you fine people would be abel to point me in the right direction.
I have the player object which moves around using the left analogue stick, great! What I want is a shield object that is directed around the player completely separate from the player (I have another game object for this) a set distance from the player pointing the direction of the right analogue stick.
I do have the right analogue stick set up and working (tested) but for the life of me and after lots of trial and just as much error (total noob) I still don't know how I'd go about this
help? please? you're awesome!
Answer by Barachiel · Aug 26, 2015 at 07:11 PM
I would suppose the easiest way to go about this would be to have an empty GameObject, with a script on it to follow the player character. Place your shield as a child of this, moving it out to the distance from the player that you want.
From there, in the script you now have on it's parent (the one to keep it at the same location as the player), rotate the object based on the input of the right stick.
The result should be that the empty GameObject will rotate while staying centered on the player, and the shield will rotate with it, staying at the distance you placed it at.
Thanks @Barachiel
I've got this so far:
public class Shield$$anonymous$$ovement : $$anonymous$$onoBehaviour {
//public float OrbitSpeed = 45.0f; //public float speed = 10.0f;
//public Transform pivot; float distance = 0.8f; Vector3 direction = Vector3.up; float speed = 1.0f; public Transform target;
// Use this for initialization void Start () {
}
// Update is called once per frame void Update () {
if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
{
direction = new Vector3(Input.GetAxis("rightH")*speed*Time.deltaTime,Input.GetAxis("rightV")*speed*Time.deltaTime, 0.0f ) ;
}
Ray ray = new Ray(target.position, direction);
transform.position = ray.GetPoint(distance);
float angle = $$anonymous$$athf.Atan2 (Input.GetAxisRaw("rightV"), Input.GetAxisRaw("rightH"));
transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward);
}
}
It starts in the right position and moves to the position where the right analogue stick is pointing, however the shield isn't rotating to face away from the player, the rotation stays static.
Also I'd like it to be a smooth rotation of the shield around the player going to a new location ins$$anonymous$$d of teleporting there
Any ideas?
Okay, so first make sure that the rotation of your empty gameobject is set to 0,0,0.
Then, place the script on it and parent the shield to it in whichever order you like, adjusting the shield, etc.
$$anonymous$$ake sure the shield is at the correct distance and rotation as if it were directly in front of the character.
Then, try something like this in the script on the empty gameobject:
using UnityEngine;
using System.Collections;
public class ShieldOrbitTest : $$anonymous$$onoBehaviour {
public Transform target;
public float rotationSpeed = 4f;
public float joystickDeadzone = 0.2f;
float xAxis;
float yAxis;
void Start () {
}
void Update () {
this.transform.position = target.position;
if ($$anonymous$$athf.Abs(Input.GetAxis("Horizontal")) >= joystickDeadzone || $$anonymous$$athf.Abs(Input.GetAxis("Vertical")) >= joystickDeadzone)
{
xAxis = Input.GetAxis("Horizontal");
yAxis = Input.GetAxis("Vertical");
}
float joystickAngle = $$anonymous$$athf.Atan2(xAxis, yAxis) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0,joystickAngle,0), Time.deltaTime * rotationSpeed);
}
}
I tested this out and it works fine, so let me know if you have any trouble with it. It's very basic and could be done better, but it's simple, works and can be expanded on.
I don't know how you have your inputs set up, so I'm just using horizontal and vertical here, which means that the wasd keys and the arrow keys will move the shield, as will the joystick, under normal input setups. You can obviously change these as needed.
In the if statement, I just use $$anonymous$$athf.Abs in front of the input check so that we can tell if the joystick is moving at all, since $$anonymous$$athf.Abs just returns a positive value, otherwise we'd need to do two checks, one for a positive value and one for a negative value.
I also added in a deadzone for the input, so that you can easily tweak it if you want high speed turning but feel it's too twitchy.
Also, if you want the shield to always return to being in front of the player when no input is given, you can just remove the if statement.