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 Tocaro · Jan 19, 2013 at 08:44 AM · anglesfacing

Making face image swap to follow hero?

So I've got this cube with a face on it rolling about, and I want to change the face texture so that it looks like it's watching the hero as he moves about.

So I've got my array of Texture2D's (4 total: up, down, left, right) to slip into my face mainTexture, but I need to figure out the angles on this thing.

I can't quite grasp it, I'd have to use the objects 'z' rotation (Since this is a 2D platformer type game), the cubes position, and the hero's position to get an appropriate angle, but I'm not sure how and not sure how that would work with replacing the images.

Since it's quite possible I'm not explaining this well (It is rather late here), here's an example.

EX:

if the cube is lying on it's right side, and the hero is to it's right it would be using the eyes upward image (because it's lying on it's side).

Okay, so any help you can give would be great, I keep going back to high school geometry making triangles in my head and mucking about with inverse tangetns, but so far nothing fits.

Comment
Add comment · Show 4
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 Imankit · Jan 19, 2013 at 09:55 AM 0
Share

There is a function LookAt.... See file:///Applications/Unity3.6/Unity.app/Contents/Documentation/Documentation/ScriptReference/Transform.LookAt.html

avatar image Tocaro · Jan 19, 2013 at 10:45 AM 0
Share

Nah, that just makes an object face another one. It's not what I'm looking for.

I need something that tells me which direction the cube object is facing relative to the hero.

avatar image whydoidoit · Jan 19, 2013 at 12:45 PM 0
Share

Is this just the "front" of the cube or does the face appear on each side of the cube?

avatar image Tocaro · Jan 19, 2013 at 10:20 PM 0
Share

It is locked to only rotate on z, so yes it's a 2d platformer with one face, and that face is only on the front.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Jan 20, 2013 at 12:48 AM

I think this is what you are looking for. First I assume you'll have a straight-on image as well as up/down/left/right images. To calculate if you want to use the straight on view, calculate the angle between the vector from the face to your object and the normal of the face. If it is below some threshold, then show the straight-on view.

As for the other four, use the DOT product between your vector to your character and the normal of the plane to project the position onto the face and then use the angle betwen up and this projection to decide which image to display.

Here is a bit of code I used for a quick test. Note goIndicator is a small sphere I used to display the projection point on the plane.

 using UnityEngine;
 using System.Collections;
 
 public class MakeAFace : MonoBehaviour {
     public GameObject goPlayer;
     public GameObject goIndicator;
     
     void Update() {
         CalcTheFace ();
     }
 
     void CalcTheFace() {
         Vector3 v3ToPlayer = goPlayer.transform.position - transform.position;
         v3ToPlayer.Normalize();
         float fAngle = Vector3.Angle (v3ToPlayer, -transform.forward);
         Debug.Log ("Angle from Normal = "+fAngle);
         
         Vector3 v3Project;
         v3Project = v3ToPlayer - Vector3.Dot(v3ToPlayer, -transform.forward) * -transform.forward; 
         goIndicator.transform.position = transform.position + v3Project;
         fAngle = Vector3.Angle (v3Project, transform.up);
         Debug.Log ("Angle between orthogonal projection and up= "+fAngle);
     }
 }

Note I used a plane created by the CreatePlane() editor script found here for my plane. It has an option to produce a vertical plane, but the face I see is the back not forward, so I have to -transform.forward for the normal. Depending on the natural orientation of the plane you are using, you will have to make appropriate transformations for the normal and up vectors.

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 Tocaro · Jan 20, 2013 at 11:17 AM 0
Share

Thanks, I'll check it out tomorrow, see how it works.

avatar image
0

Answer by lukasvogl · Jan 19, 2013 at 01:23 PM

couldn´t you create two polyplanes for the eyes and translate them? it would be way faster and simpler then storing and switching textures. and the calculation of the corect position should be fairly easy too.

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 Tocaro · Jan 19, 2013 at 10:17 PM 0
Share

I could but that's not what I want to do, and not really what the questions was asking. I have four different textures (UP, DOWN, LEFT, RIGHT) and I want to apply those so it seems like the Cube is watching the Hero.

Each texture is different, it's not just eye positioning, it's got a different mouth, eyebrows and all that. So I can't just move the eyes.

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

13 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

Related Questions

Angle between two objects' facing direction 2 Answers

Setting rotation Angle / rotation question 1 Answer

Rotation around a moving object. 0 Answers

How to check object "rotated" X degrees 1 Answer

Vector3.Angle() 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