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 AGP · Dec 29, 2010 at 06:42 PM · rotationlookatmatrix

Transform.LookAt and Back!

How do I make a character turn in the direction of the camera once (to attack player), then turn back to the direction at which it was looking? The following attempt failed:

function Update () {
if (calculateDistance(GameObject.Find("Main Camera"), this.gameObject) > 4f) {
    if (rotation.y != 10001) {
        //this.gameObject.transform.eulerAngles = rotation;
        //this.gameObject.transform.LookAt(oldForward, oldUp);
        this.gameObject.transform.localRotation = rotation;
        addWalkingForce();//MOVE FORWARD AGAIN
        rotation.y = 10001;
    }
    this.animation.Play ("Walks");
}
else {
    this.animation.CrossFade ("Attacks");
    oldForward = this.gameObject.transform.forward;
    oldUp = new Vector3(Vector3.up.x, Vector3.up.y, Vector3.up.z);
    rotation = this.gameObject.transform.localRotation;
    rigidbody.velocity = Vector3.zero;//KILL FORWARD MOVEMENT
    this.transform.LookAt(GameObject.Find("Main Camera").transform, Vector3.up)
}

Comment
Add comment · Show 2
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 Justin Warner · Dec 29, 2010 at 08:20 PM 0
Share

Get the rotation before, store it, then make it rotate, then make it rotate back to the original position...

avatar image AGP · Dec 29, 2010 at 10:00 PM 0
Share

That is what I did, as the code shows.

1 Reply

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

Answer by AGP · Dec 31, 2010 at 11:34 PM

I'm surprised no one had anything to say about it. But the following worked. I added the time test to prevent it from storing the rotated forward before the animation could be said to be "Walks" (as it was cross fading, I suppose). Other than this little bit, the only thing I can tell was wrong was that transform.forward gives you the worldspace vector but not the actual position (hence the addition). I hope this helps someone.

PS: How frustrating is this system of trying to get code colored and indented here? Come on, guys, come up with brackets, or something.

private var oldForward : Vector3;

private var lastTimeStoredForward : float;

function calculateDistance(from : GameObject, to : GameObject) : float { var origin : Vector3 = from.transform.position; var destination : Vector3 = to.transform.position; return Mathf.Sqrt((origin.x-destination.x)(origin.x-destination.x) + (origin.y-destination.y)(origin.y-destination.y) + (origin.z-destination.z)*(origin.z-destination.z)); } function addWalkingForce() { this.rigidbody.AddForce (this.gameObject.rigidbody.transform.forward*20); } function Start() { addWalkingForce(); oldForward.y = 10001; lastTimeStoredForward = -99999; } function Update () { if (calculateDistance(GameObject.Find("Main Camera"), this.gameObject) > 4f) { if (oldForward.y != 10001) { this.gameObject.transform.LookAt(oldForward, Vector3.up);

        addWalkingForce();
        oldForward.y = 10001;
    }
    this.animation.Play ("Walks");
}

else if (animation.IsPlaying("Walks")) { if (lastTimeStoredForward+1 < Time.time) { oldForward = new Vector3(this.gameObject.transform.position.x+transform.forward.x,transform.position.y, this.gameObject.transform.position.z+transform.forward.z);//WHY DOESN'T THIS WORK? lastTimeStoredForward = Time.time; } this.animation.CrossFade ("Attacks"); rigidbody.velocity = Vector3.zero; var toLook : Vector3 = GameObject.Find("Main Camera").transform.position; toLook.y = this.gameObject.transform.position.y;//DO NOT ROTATE AROUND X-AXIS this.gameObject.transform.LookAt(toLook, Vector3.up); } } function OnCollisionEnter(collision : Collision) { if (collision.gameObject.name.StartsWith("ZombieLevel")) { Debug.Log("Collided against level."); this.gameObject.transform.Rotate(0, 180, 0); } }

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

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

[HELP] Rotate Bullet Script 0 Answers

Rotation Reset 0 Answers

Need help to modify Space.World rotation script. 1 Answer

Z rotation locks to certain value randomly? 0 Answers

Object won't rotate z axis on second click? 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