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
1
Question by Atrius · Apr 14, 2012 at 02:11 AM · c#quaternion

Rotate Door Object Away from Player

First some context. My map is a tile based system with randomly generated walls and doors. All of these pieces are prefabs instantiated by script and placed in their proper location and orientation.

Right now I have doors. On an input key it fires a raycast that checks for the collision with objects on the Interactable layer. When one is found it runs a script on the door GameObject for it to rotate open.

They are currently rotating using a Quaternion.Slerp with a From of the current transform.rotation of the GameObject and the To of a transform.rotation of a public GameObject assigned to the tile prior to building.

This works fine, however it's a hardcoded direction of rotation. What I am trying to do is determine a way to programatically create the destination Quaternion/Transform so that the door always rotates AWAY from the player. Right now it has a 50/50 chance of hitting the player in the face.

The info I have available is a RayCastHit, Player GameObject, Door GameObject. I have tried doing things like taking the ray.normal and flipping it 180 degrees, which gives a "forward" but that forward isn't oriented up so it rotates the door correctly while also rotating it down into the floor.

I feel like this should be a lot simpler than I am making it. Anyone have thoughts?

private enum ActionStates { CLOSED, OPEN, CLOSING, OPENING, } private ActionStates actionState; private float timer = 0.0f; public Transform transOpen; private Transform transStart; public bool isUsable = true; public float timeMult = 0.5f;

void Start () { actionState = ActionStates.CLOSED; transStart = transform;
}

void Update () { if (actionState == ActionStates.OPENING) { timer += Time.deltaTime * timeMult; transform.rotation = Quaternion.Slerp(transStart.rotation,transOpen.rotation, timer); if (timer >= 1.0f) { actionState = ActionStates.OPEN; } }

public void DoUse() { if (actionState == ActionStates.CLOSED) { DoOpen(); } if (actionState == ActionStates.OPEN) { DoClose(); } }

public void DoOpen() { actionState = ActionStates.OPENING; timer = 0.0f; }

public void DoClose() { actionState = ActionStates.CLOSING; timer = 0.0f; }

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

1 Reply

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

Answer by aldonaletto · Apr 14, 2012 at 03:27 AM

I would do things in a different manner:
1- Do a raycast from the player (I suppose that's what you're already doing);
2- If the object hit is a door, use SendMessage to call a door function that opens the door - pass the player position as the parameter;
3- Based on the player position, this routine decides to which side rotate;
4- The door rotates a fixed angle (say, -120 or 120 degrees) around the Y axis with Rotate, or by setting eulerAngles.y;

In practical terms, the whole thing would be like this:

  • In the player script:

...
if (Physics.Raycast(transform.position, transform.forward, out hit)){
  if (hit.transform.tag == "Door"){
    hit.transform.SendMessage("OpenDoor", transform.position);
  }
}
...
- In the door script:

public float speed = 90; // door speed in degrees per second public float openAngle = 120; // opening angle in degrees

Vector3 curAngle; float startAngle; float targetAngle;

void Start(){ curAngle = transform.eulerAngles; startAngle = curAngle.y; // save the startAngle (closed) targetAngle = startAngle; }

void OpenDoor(playerPos: Vector3){ if (actionState == ActionState.CLOSED){ // only open a closed door! Vector3 dirDoor = transform.position - playerPos; float dot = Vector3.Dot(dirDoor, transform.right); if (dot > 0){ // if door opens to the wrong side, use dot < 0 targetAngle = startAngle + openAngle; } else { targetAngle = startAngle - openAngle; } actionState = ActionState.OPEN; } }

void Update(){ // rotate gradually door to targetAngle, if different of curAngle: curAngle.y = Mathf.MoveTowards(curAngle.y, targetAngle, speed * Time.deltaTime); transform.eulerAngles = curAngle; }

void CloseDoor(){ // closing the door is much easier! targetAngle = startAngle; actionState = ActionState.CLOSED; } There are some things to consider:
1- This code assumes that the door's Y passes through the hinges and Z points to the handle; if X points to the handle side, use Dot((dirDoor, transform.forward).
2- If the door is opening to the wrong side, invert the dot comparison (dot < 0).

Comment
Add comment · Show 2 · 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 Atrius · Apr 15, 2012 at 02:41 AM 0
Share

Thanks. The above code I have is actually on the door object and I call it with Send$$anonymous$$essage just as you say. I just use a standard of "IsUsable" and "DoUse" for all objects that can be interacted, so my input code is simpler. The piece about subtracting the transform.position and playerPos and doing the dot product is what I was having trouble figuring out. Thanks!

avatar image aldonaletto · Apr 15, 2012 at 03:26 AM 0
Share

Glad to know it helped! Notice that I'm getting the dot product of two vectors: the player->door vector and a door related vector. This value is positive if both vectors are at less than 90 degrees of each other, and negative if the angle is greater than 90 degrees. $$anonymous$$eep this in $$anonymous$$d if you have to chose another vector than door.right.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Issue with Adding Quaternions 1 Answer

Quaternion.RotateTowards Problem 2 Answers

[Solved] Help on a rotation issue for a Top-Down view? 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