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 collederas · Nov 07, 2015 at 04:45 PM · rotatearoundlockpivotrotatearoundpivot

Rotate player around enemy (Lock system)

I am trying to make a lock-on-target system for a melee combat game. The idea is that when you lock an enemy you switch to a movement system where, if pressing left or right, you rotate around him. Pressing forward or back will move the player closer or farther from the enemy (de facto increasing or decreasing the radius of the rotation).

I am almost there. I say almost because my character does spirals around the enemy, not circles. The idea is the following:

 // Gather the direction of the analog stick.
 Vector3 stickDirection = new Vector3 (horizontal, 0f, vertical);
 speedOut = stickDirection.magnitude;

 // Calculate the relative position player/target and kill the y.
 Vector3 lineToTarget = target.position - player.position;
 lineToTarget.y = 0f;

 // Create a rotation to align with the vector player --> target and apply the rotation to the stick direction.         
 Quaternion movementRotation = Quaternion.LookRotation(lineToTarget);
 Vector3 targetStickDirection = movementRotation * stickDirection;

 // Apply speed.
 directionOut = targetStickDirection * speedOut;

At this point the direction of the stick vector is relative to the enemy. This direction is used to create the final rotation for the player:

 targetRotation = Quaternion.LookRotation (direction, Vector3.up);
 Quaternion newRotation = Quaternion.Lerp (player.rotation, targetRotation, speedDirection *  Time.deltaTime);
 
 transform.rotation = newRotation;

The speed is also passed to the Animator that simply moves the player forward based on it.

So, to recap:

  • The code above is used to define the direction of the stick relative to the targeted enemy.

  • The direction used to rotate the player

  • The animator pushes the player forward

Result: The player orbits around the enemy. BUT in a spiral! What am I doing wrong?! Or, better, how would you achieve a "Lock on target" system (kinda like Dark Souls)?

Thanks for any effort guys!

[EDIT]

Please refer to the following video that clarifies the situation:

https://www.youtube.com/watch?v=q1Yn2xdc-jU&feature=youtu.be

The red line is the relative vector player --> target The green line is the directionOut Vector. The blue line is the player.forward Vector.

You see that the latter it is pointing slightly outwards. I cannot get why! It should be overlapping the green one!

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
0

Answer by feliperapussi · Nov 07, 2015 at 07:20 PM

why you do not use the transform.RotateAround ?

if ( input.GetKey ("up") ) { transform.RotateAround ( target.position , direction , speed ); }

direction >>> vector3.up\down\right\left speed >>> some value * Time.deltaTime

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 collederas · Nov 08, 2015 at 12:47 PM 0
Share

It surely works if you only want to rotate around the target (only passing Vector3.up and Vector3.down as direction, though, not left and right which would result in rotating the player in the air :D).

But in any case this solution doesn't take in consideration the user's different pressure on the stick..:/

avatar image Grocho collederas · Feb 17, 2018 at 07:47 PM 0
Share

Hello there, it rotates based on the Vector you gave in the direction variable.

What I would do, is create two directions, leftDir and rightDir; This would be the code then: float speed = 2f; Vector3 leftDir = vector3.left; Vector3 rightDir = vector3.right; if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A)) { transform.RotateAround(target.position, leftDir, speed); } else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D) { transform.RotateAround(target.position, rightDir, speed); }

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

34 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 avatar image avatar image avatar image

Related Questions

Manual RotateAround 0 Answers

Is there a RotateAround functionality for Rigidbody 2D's? 1 Answer

Deformation of child object when rotating around a rotatable parent object 0 Answers

What the angle argument means in the Transform.RotateAround function? 1 Answer

How do I rotate an object around around another, following mouse position and facing it? 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