- Home /
Play movement and restrictions - Similar to a window wiper
Hello I was looking for some help on how to set my player game objects movements. Essential the player is a stationary turret in the middle of the map and I just need to make it move left and right.
Now I have made it so that is rotates but it is currently rotating 360 degrees and I simply need to to go 90 degrees to the right and 90 degrees to the left...
This is what I currently have for script and it doesn't seem to be working correctly - please help:
using UnityEngine; using System.Collections;
public class playerMovement : MonoBehaviour {
public float speed ;
public float rotationSpeed;
//transform
Transform myTrans;
//object position
Vector3 myPos;
//object rotation
Vector3 myRot;
//object rotation
float angle;
// Use this for initialization
void Start ()
{
myTrans = transform;
myPos = myTrans.position;
myRot = myTrans.rotation.eulerAngles;
}
void FixedUpdate ()
{
//rotate object Right & Left
if (Input.GetKey (KeyCode.RightArrow)) {
myRot.z -= rotationSpeed;
}
if (Input.GetKey (KeyCode.LeftArrow)) {
myRot.z += rotationSpeed;
}
if (transform.eulerAngle.x > 90) {
transform.eulerAngle.x = 90;
}
myTrans.position = myPos;
myTrans.rotation = Quaternion.Euler (myRot);
}
}
Answer by EdocGaming · Mar 08, 2014 at 01:46 AM
I think you need to setup a "Hinge Joint" that way you can setup the limits min and max. do this to the gameobject in the editor before writing a control code for it.
if you need more help I can add a sample later on
I did use Hinge Joint before and it stopped the movement of the player...
Could you please provide a sample when you get a chance - not sure why it's not working on my end but the player object isn't moving now. Just to give you an idea of how the play is constructed at this point it is simply a cylinder and a sphere pieced together with a joint and the sphere is the object that moves and pulls the cylinder around with it. (*Hope that makes sense).
Thank you for your help thus far, it is greatly appreciated!
I'm working it out for you now.
Btw what do I do to add the code like you have on here?
You can just copy and paste it out of a text editor and it will do it automatically! If not type it and apply indents that typically does the trick!
hey, the button that says:
101
010
above the answer box lets you format your text like code.
Answer by fifthknotch · Mar 08, 2014 at 04:13 AM
Just move your if statement:
void FixedUpdate ()
{
if (transform.eulerAngle.x >= 90) {
//rotate object Right & Left
if (Input.GetKey (KeyCode.RightArrow)) {
myRot.z -= rotationSpeed;
}
}
if (transform.eulerAngle.x <= -90) {
if (Input.GetKey (KeyCode.LeftArrow)) {
myRot.z += rotationSpeed;
}
}
}
So the same thing occurs when I first attempted to get my object to move - the object stop rotating completely and stays fixed... Here is my code:
using UnityEngine;
using System.Collections;
public class player$$anonymous$$ovement : $$anonymous$$onoBehaviour
{
public float speed;
public float rotationSpeed;
//transform
Transform myTrans;
//object position
Vector3 myPos;
//object rotation
Vector3 myRot;
//object rotation
float angle;
// Use this for initialization
void Start ()
{
myTrans = transform;
myPos = myTrans.position;
myRot = myTrans.rotation.eulerAngles;
}
void FixedUpdate ()
{
if (transform.eulerAngles.x >= 90) {
//rotate object Right & Left
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
myRot.z -= rotationSpeed;
}
}
if (transform.eulerAngles.x <= -90) {
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) {
myRot.z += rotationSpeed;
}
}
}
}
Again thank you for your help this far, much appreciated!
Your answer
Follow this Question
Related Questions
Child Sprite Skewing 1 Answer
Character Movement won't work properly because of the rotation 1 Answer
Rotating sprite towards another sprite 1 Answer
Rotate & Translate 1 Answer