Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ACQuo1001 · Mar 07, 2014 at 10:15 PM · rotate objectmovementsrelative rotationtransform.restrictions

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);

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ACQuo1001 · Mar 08, 2014 at 02:18 AM 0
Share

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!

avatar image EdocGaming · Mar 08, 2014 at 02:28 AM 0
Share

I'm working it out for you now.

Btw what do I do to add the code like you have on here?

avatar image ACQuo1001 · Mar 08, 2014 at 03:24 AM 0
Share

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!

avatar image Romano · Mar 08, 2014 at 03:31 AM 0
Share

hey, the button that says:

101

010

above the answer box lets you format your text like code.

avatar image ACQuo1001 · Mar 08, 2014 at 03:42 AM 0
Share

Oh shoot - that's cool thanks for letting us know!

Show more comments
avatar image
0

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;
        }
      }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ACQuo1001 · Mar 08, 2014 at 06:37 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Rotate object via script like in editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges