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
2
Question by twoface262 · Jul 16, 2012 at 06:57 PM · rotationjavascriptenemy

How to Rotate on one axis?

So I'm trying to rotate an object on only the Y axis here is the code I got.

   var lookDirectiony = player.position.y - transform.position.y;
     transform.Rotate(0,lookDirectiony,0);


I've tried several ways to do this but the enemy will normally start flying around. How do I only rotate it on ONE axis and make sure it stays on the ground? It already has a rigidbody with gravity and mass. That code that I made doesn't rotate it for some reason.

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

4 Replies

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

Answer by aldonaletto · Jul 17, 2012 at 02:23 AM

You're confusing things: lookDirectiony is a distance, while Rotate requires an angle in degrees. If you want the enemy to look in the player direction, you can just use LookAt like DaveA suggested:

 transform.LookAt(player);

But this may tilt the enemy when the player is at a different height, thus a little more coding may be necessary:

 var lookDir = player.position-transform.position;
 lookDir.y = 0; // keep only the horizontal direction
 transform.rotation = Quaternion.LookRotation(lookDir);

this will align the enemy forward direction (the blue axis) to the vector lookDir, which became strictly horizontal because its Y coordinate was zeroed - thus effectively rotating the enemy strictly around the Y axis.

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 twoface262 · Jul 17, 2012 at 04:13 AM 0
Share

The problem I was having was keeping the AI on the ground. I figured that it'd be because of the rotation, but it wasn't. So I've fixed all the problems. Thanks for the feed back and helping me solve my problem!

avatar image
1

Answer by herman111 · Jul 22, 2014 at 01:45 PM

 using UnityEngine;
 using System.Collections;
 
 public class RotateOBJ : MonoBehaviour {
 
     // Speed it goes around.
     public int rotateSpeed = 5;
         
 
     void Update () {
         // Rotate around the (z) axis.
         transform.Rotate(0 ,0 ,rotateSpeed * Time.deltaTime);
 
     }
 }
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 lll4louis · Apr 20, 2019 at 03:22 PM 0
Share

Finally! Something that works only on the Z axis!

avatar image
1

Answer by Claudioteles85 · Apr 11, 2019 at 12:31 PM

By default when you rotate, all rotate.x,y,z axis moves. This is especially evident when rotate.x does not equal 0. If you want to rotate the game Object relative to the World and not relative to Self make use to add Space.World at the end of transform.Rotate

 float horizontalMove = moveSpeed * Input.GetAxis("Mouse X");
 transform.Rotate(0, horizontalMove, 0, Space.World);







Comment
Add comment · 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
0

Answer by DaveA · Jul 16, 2012 at 08:38 PM

This may be some help: http://docs.unity3d.com/Documentation/ScriptReference/Transform.LookAt.html

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 twoface262 · Jul 17, 2012 at 01:49 AM 0
Share

No help. Thanks though.

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

9 People are following this question.

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

Related Questions

Rotating on X and Z axis only 2 Answers

Slerp Problem?: Enemy in Constant Rotation! 1 Answer

camera rotation 0 Answers

Move the Object to Camera respective. 1 Answer

How to lock Y Rotation 3 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