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 RexoniuM · Dec 16, 2012 at 04:39 PM · rotationobject

Rotate only Y-axis of some object toward another

I have rotation of some object to be (270,yRotation,0). How to achieve my object only rotates around y-axis toward some another object in move?

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

3 Replies

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

Answer by clunk47 · Dec 16, 2012 at 05:00 PM

Here is a really easy way to do this. Attach this script to a turret or something that is going to look at your player. Just drag the player, or whatever you want the "turret" to look at, onto the slot for "Player" in the inspector. This example is in C#.

 using UnityEngine;
 using System.Collections;
 
 public class EXAMPLE : MonoBehaviour 
 {   
     public Transform player;
     Vector3 target;
     
     void Update () 
     {
         target = new Vector3(player.position.x, this.transform.position.y, player.position.z);
         transform.LookAt(target);
     }
 }
 
Comment
Add comment · Show 6 · 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 Owen-Reynolds · Dec 16, 2012 at 05:12 PM 2
Share

Notice the trick here is to change the target Y to your own Y. That way LookAt doesn't want to look up or down.

It looks like the OPs object might be looking up, so the trick would be to change the target Z into your own Z.

avatar image RexoniuM · Dec 17, 2012 at 11:33 AM 1
Share

Rotation affects all three axis... I don't know why.

avatar image RexoniuM · Dec 17, 2012 at 12:47 PM 1
Share

Object follows player, however, it's in disposition, facing upward ins$$anonymous$$d of forward. What may be the cause to it?

avatar image clunk47 · Dec 17, 2012 at 04:15 PM 3
Share

Be sure the axes of your object are correct (X is left / right, Y is up / down, Z is forward / back. Sometimes if you have an imported model(especially fbx or blender) the axes can be off. If this is the case, you would need to create an empty gameObject and make it the parent of your model, then remove the script from your model and attach it to the model's new parent (the new empty gameObject).

avatar image RexoniuM · Dec 18, 2012 at 08:41 AM 2
Share

Oh, dear, after some time I have finally managed to work this out :) The code I applied was this var direction = player.position-transform.position; direction .y = 270;
transform.rotation = Quaternion.LookRotation(direction ); And it works just smooth and perfect as I wanted it to work. :)

Show more comments
avatar image
1

Answer by Aloan · Apr 06, 2014 at 05:22 AM

Clunk47! Thank you! your codes does perfectly what I wanted! Now my quad trees and quad people all rotate to me nicely. No more tilting like they were falling or leaning over! this is marvelous! I've been looking for this code for days!

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 clunk47 · Apr 06, 2014 at 05:28 AM 0
Share

Glad I could help dude :D

avatar image
0

Answer by Map-Builder · Dec 17, 2012 at 02:13 PM

 transform.rotation = Quaternion.LookRotation(targetObject.position - transform.position, Vector3(0,1,0));
 // Vector3(0,1,0) keep heads up along that vector (y+)

Should work properly

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 Owen-Reynolds · Dec 17, 2012 at 03:40 PM 1
Share

Right idea, but not helpful numbers.

LookAt has a "free spin" after it aims. You control it with a 2nd input saying which way counts as "up." If you leave it out, it automatically uses `Vector3(0,1,0)`.

If the object is aimed mostly up, something like `Vector(0,0,-1)` would try to have the top part face the camera (well, if the camera is south of you.)

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

14 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

Related Questions

A node in a childnode? 1 Answer

Rotate GameObject using Coroutine 2 Answers

object rotation to another vector not changing 0 Answers

problem with object rotation 1 Answer

How do I delete an object in my Project 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