Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by tonsillectomy · Oct 24, 2015 at 11:51 AM · anglemovinglimit

Moving to certain direction

Hi. Kinda easy question but how can I make an object move to a certain direction relative to object's angle? Basically I just want to replace "transform.forward" to "transform.forward - 45 degrees".

I have an object that is following another object (lets call it guide) and I would like to limit the object's maximum turning angle to 45 degrees, so when the angle between the object and the guide exceeds 45 degrees, the object cannot turn any tighter (moves to direction forward - 45 degrees instead of following the guide).

I've got everything else figured out except for that, even though that should be the easiest part :D

Comment
Add comment · Show 5
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 Statement · Oct 24, 2015 at 12:47 PM 0
Share

Your question is hard to understand. Are you rotating object or how is it following guide? I mean, it gets strange to think about object always moving forward, clamped to 45 degree translation in two axis based on the location of guide if object doesn't rotate. And if object rotates, wouldn't it make sense to limit the rotation speed of object to avoid hard turns ins$$anonymous$$d of trying to modify forward direction?

Please explain with what you have (code or project or images help) and what you want to achieve is without getting technical (for example "guide moves around, object is trying to follow guide but should not make hard turns").

If you want to move "forward and right" (45 degrees right of forward) you could do something like:

 Vector3 fwdAndRight = Vector3.Normalize(Vector3.forward + Vector3.right);
 Vector3 direction = transform.TransformVector(fwdAndRight);
 transform.position += direction;
avatar image tonsillectomy Statement · Oct 24, 2015 at 01:49 PM 0
Share

Thanks for quick reply and sorry for my bad explanation :D

Lets say the object is a boat and its always rotating towards the guide. Guide moves around, boat is following guide and stops when it reaches the guide. The problem is if the boat is near the guide and the guide changes radically the movement direction (to behind the boat for example) the boats's rotation cannot keep up with the guide and the boat starts to move sideways. I'm trying to limit the moving angle so that this wouldn't happen. This is hard to explain but hopefully this code helps a bit:

void $$anonymous$$ove(float angleToGuide, float limitedAngle, float limitedAngleNegative) {

     // object slows down when it gets closer to guide
     if (angleToGuide <= 30 && angleToGuide >= -30 && distanceToGuide > 0 && distanceToGuide <= 4) {
         maxSpeed = 1f;
         moving = true;
     } else if (angleToGuide <= 30 && angleToGuide >= -30 && distanceToGuide > 4) {
         maxSpeed = 3f;
         moving = true;
     }
     
     
     if (distanceToGuide == 0) {
         torque = 0f;
         moving = false;
     }
     
     //accelerating and deccelerating
     if (torque < maxSpeed && moving) {
         torque += 0.5f * Time.deltaTime;
     } else if (torque > maxSpeed && moving) {
         torque -= 1f * Time.deltaTime;
     }
     
     //here is where I try to limit the turning angle
     //if (moving) {
     if (moving && angleToGuide > -30 && angleToGuide < 30) {
         transform.position = Vector3.$$anonymous$$oveTowards (transform.position, target.position, moveStep);
     } else if (angleToGuide <= -30 && moving) { 
       //  move object to -limited direction
     } else if (angleToGuide >= 30 && moving) { 
     //    move object to limited direction
     }
 }

}

avatar image Statement tonsillectomy · Oct 24, 2015 at 02:07 PM 0
Share

Uh, is this kind of what you describe?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Statement · Oct 24, 2015 at 03:22 PM

Create a function that calculates where the guide should be. Note: I don't know which coordinate system your game uses. This assumes that the boat is moving along the Y plane (Y is up, X and Z are directions the boat moves in).

 Vector3 GetAdjustedGuide(float angleToGuide)
 {
     // The black arrow in comment graphic
     Vector3 offset = target.position - transform.position; 
     
     // Clamp the angle between -30 and 30 degrees
     float clampedAngle = Mathf.Clamp(-30, 30, angleToGuide);
     
     // Create an euler rotation on Y axis to be within -30 to 30 degrees
     // Depending on your game setup, you may need to choose a different axis, like the Z axis.
     Quaternion clampedRotation = Quaternion.Euler(0, clampedAngle, 0);
     
     // Rotate a forward direction with the same length as offset
     // Depending on your game setup, you may need to choose a different forward, like up.
     Vector3 clampedDirection = clampedRotation * new Vector3(0, 0, offset.magnitude);
     
     // Transform the direction to take into account the boats current rotation and positon
     Vector3 adjustedGuide = transform.TransformPoint(clampedDirection);
     return adjustedGuide;
 }

And then where you had your movement code, it could look something like this:

 if (moving)
 {
     // Move toward adjustedGuide
     transform.position = Vector3.MoveTowards (transform.position, 
                                               GetAdjustedGuide(angleToGuide), moveStep);
 }
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

31 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 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

Accelerometer moving limit. Help me please! 1 Answer

How to make the object return to its last angle (before the angle limit) go back no so abruptly ? 0 Answers

How do i limet angles of a raycast? 0 Answers

how can i limit position between 2 object ?? 1 Answer

Side movement 3d runner 0 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