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
1
Question by Nolan Encarnacion · Oct 08, 2010 at 12:39 AM · rotationmovementmovetranslatetranslation

How do I translate my ship left and right and have it rotate at the same time?

SO essentially Im working on a top down 3d shooter, and I have a ship that I want to move side to side and up and down, I HAVE THIS WORKING, but there is a new feature that I would like to add, I would like the ship to rotate towards the direction that it is turning and then when the user is done turning it goes back to its original rotation orientation.

Here is my code so far

using UnityEngine; using System.Collections;

public class PlayerScript: MonoBehaviour { // players lives private int playerLife = 5; private float speed = 8.0f; private int missleCount = 3; public float rotateSpeed;

public static int playerScore; //public static int missles;

public Rigidbody projectileBullet; public Rigidbody projectileMissle;

void OnGUI()//Works with the GUI interface { //Score that shows on screen GUI.Label(new Rect(10,10,200,50), "Score: " + playerScore);

 //Players Life
 GUI.Label(new Rect(10,30,200,50), "Lives: " + playerLife);

 GUI.Label(new Rect(10, 50, 200, 50), "Missles: " + missleCount);

}

public int getPlayerLife { get{ return playerLife;} }

public float getSpeed { get{ return speed;} }

// Update is called once per frame void Update () {

 //amount to movement
 float movementHorizontal = (speed * Input.GetAxis("Horizontal")) * Time.deltaTime;

 float movementVertical = (speed * Input.GetAxis("Vertical")) * Time.deltaTime;

 transform.Translate(Vector3.right * movementHorizontal);

 //translate the player Horizontaly
 transform.Translate(Vector3.right * movementHorizontal);

 //translate vertically
 transform.Translate(Vector3.up * movementVertical);

 if (transform.position.x <= -7.5)
 {
     transform.position  = new Vector3(7.4f, transform.position.y, transform.position.z) ;
 }

 else if (transform.position.x >= 7.5)
 {
     transform.position = new Vector3(-7.4f, transform.position.y, transform.position.z);
 }


 if (Input.GetKeyDown(KeyCode.Space))
 {
     Rigidbody cloneBullet;
     cloneBullet = Instantiate(projectileBullet, transform.position, transform.rotation) as Rigidbody;

 }

 if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
 {

     Debug.Log(missleCount);
     if (missleCount >= 0 && missleCount != 0)
     {
         --missleCount;
         Rigidbody cloneMissle;
         cloneMissle = Instantiate(projectileMissle, transform.position, transform.rotation) as Rigidbody;
     }

 }

}

}

Comment
Add comment · Show 1
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 · Oct 08, 2010 at 12:44 AM 1
Share

Please consider giving your question a title that indicates what the problem is, rather than YELLING AT US ABOUT YOUR BIG PROBLE$$anonymous$$. Cheers.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Loius · Oct 08, 2010 at 02:27 AM

Rotation is accomplished through transform.rotation. You can find all you need about rotation in the Script Reference; just search for "rotation".

While your player is moving right, rotate towards +X* on the ship's up axis; while moving left, go towards -X*.

Comment
Add comment · Show 3 · 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 Nolan Encarnacion · Oct 08, 2010 at 03:51 AM 0
Share

Thank's I'll try it out!

avatar image Nolan Encarnacion · Oct 08, 2010 at 04:06 PM 0
Share

I have another question? Sorry T.T How do you reset the ships rotation position after it has translated and rotated towards the direction?

avatar image burtonposey · Mar 28, 2011 at 12:39 AM 0
Share

You probably don't need to "reset" your rotation as much as you need to just move the ship forwards or up along the current rotation.

So by default, if your ship is facing up, then any rotations you apply you can Translate the ship up as shown in the example on the following page, http://unity3d.com/support/documentation/ScriptReference/Transform.Translate.html

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

No one has followed this question yet.

Related Questions

Stop Object From Moving After Key Release 2 Answers

First person controller not moving properly after rotation 1 Answer

Moving an object based on the position of another object 2 Answers

Trying to make a Projectile Rotate and Move Forward.. 1 Answer

How to stop coroutines or functions 2 Answers


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