Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 JordanH11 · Dec 01, 2013 at 02:53 AM · 2dplayerrotateplanetowards

How to make plane rotate towards player and keep upright?

Hi, I'm trying to make my 2d plane rotate towards the player GameObject, but stay at 90 degrees on the x axis... Any idea on how to do this? I've tried a few things, but can't seem to freeze the x axis.

 public class FacePlayer : MonoBehaviour {
     
     private Transform player;
     
     void Update() {
 
         player = GameObject.Find ("Player").transform;
 
         transform.LookAt(player);
     }
 }

This is the code I have so far, any help at all will be appreciated, thanks.

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 Crazydadz · Dec 01, 2013 at 05:17 AM 0
Share

Don't know if this can help.

1 Reply

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

Answer by robertbu · Dec 01, 2013 at 05:36 AM

There are a couple of ways this question can be interpreted. I'm going to assume that what you have is a Unity Plane game object that you've rotated on on the 'x' axis so that it is vertical. And you want to rotate it around the the world 'Y' so that it is billboarded towards the player. The first step is to get a plane/quad that is naturally vertical. That is, it is vertical when the rotation is (0,0,0). Here are a number of ways to solve that problem:

  • Instead of a Plane game object, use the new Quad game object (introduced in Unity 4.2).

  • Use the CreatePlane editor script from the Unity Wiki, and specify 'Vertical' for the orientation when creating the plane.

  • Use an empty game object. Rotate the Unity Plane to a vertical orientation. Set the position as (0,0,0). Create an empty game object at position (0,0,0). Make the Plane a child of the empty game object. The script goes on the empty game object.

  • Create the plane in a 3D authoring program with a vertical orientation.

  • Use the RotateMesh editor script here to create a clone with the correct orientation.

For the Quad solution and the CreatePlane solution, the visible size is facing -Z (back). For the other solutions the visible side could be either -Z (back) or +Z (forward). To allow the object to rotate on only the 'y' axis, the trick is to bring the viewing direction parallel with the xz plane before doing a look rotation. Here is the script:

 using UnityEngine;
 using System.Collections;

 public class FacePlayer : MonoBehaviour {
     
     private Transform player;
 
     void Start() {
         player = GameObject.Find ("Player").transform;
     }
     
     void Update() {
         Vector3 v3 = player.position - transform.position;
         v3.y = 0.0f;
         transform.rotation = Quaternion.LookRotation(-v3);
     }
 }

If the plane/quad is facing forward instead of back, use 'v3' instead of '-v3' in the last line. Also you don't want to do the 'GameObject.Find()' every frame if you can avoid it.

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 JordanH11 · Dec 01, 2013 at 11:35 AM 0
Share

Thanks a lot, I've got it working now, you were a ton of help :)

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

17 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

Related Questions

Quaternion Rotate Towards Y Value 1 Answer

Camera rotation around player while following. 6 Answers

rotate 2d object toward movement 1 Answer

How to make player rotate just for specific angle? 0 Answers

Rotate object around player when space is pressed. 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