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 Benjam · Mar 31, 2013 at 03:14 PM · slerpxbox controllerarc

Trying to move an object in an arc around the player

Hey,

I am trying to get my sort of gravity gun to work. The aim is to have the player draw an object (tractor beam style which i have working) and then to allow him to rotate the object around him to protect himself and then fire it off into certain directions.

You are going to be able to control the arc by using the XBOX right analog stick. The issue i am having is getting the object you have in your tractor beam to be controlled in an arc manner by actually using the right analog stick.

I have tried using rotate around but this is a fixed rotation and i need it to be updated in real time. Here is some of my code and explanations as to why i have used certain bits.

This line assigns the value of the right sticks to a variable to control the object and multiplies it by 360 to gain a 360 degree rotation of the thumb sticks:

 rThumbArc = (Input.GetAxis("360_LThumbButton_X") - Input.GetAxis("360_LThumbButton_Y")) * 360;

This function is where i am struggling. I have tried RotateAround which gives me good results except i cannot control it with the thumbstick:

 target.transform.RotateAround (currentPlayer.transform.position, Vector3.left, 100 * Time.deltaTime);

I then looked into circle geometry and found some pretty solid information about how to calculate points on an arc of a circle so i input all of that but cannot assign the values to the target objects transform.position. I have seen SLERP used a lot when i have googled but i can't get my head around how to make it work and how it actually works with to.positions and from.positions.

Here is my code to calculate the arc points:

 void controllerArc()
 {
     //Set the two centre coordinates to be the players Y and Z positions
     //Using only Y and Z to keep the circle in which the object travels in 2 dimensions
     float aCentre = transform.position.y;
     float bCentre = transform.position.z;
     float radius;
     float diameter;
     float xyDistance = 3.0f;
     float PI = Mathf.PI;
     float targetPos;
     
     //Maths equations going down
     radius = ((xyDistance - aCentre)*(xyDistance - aCentre)) + ((xyDistance - bCentre)*(xyDistance - bCentre));
     radius = Mathf.Sqrt(radius);
     diameter = radius * 2;
     
     if(Input.GetAxis("360_XButton") > 0.0)
     {
         target.transform.RotateAround (currentPlayer.transform.position, Vector3.left, 100 * Time.deltaTime);

         //target.transform.position = Vector3.Slerp(currentPlayer.transform.position , target.transform.position, xyValue);
         
         targetPos = diameter * PI * rThumbArc/360;
         //targetPos.z = diameter * PI * rThumbArc/360;
         
         print(targetPos);
         
         //target.transform.position = transform.position;
     }
 }

The basic idea is shown in this expertly drawn paint diagram:

alt text

Can anyone help me understand how to use SLERP if this is in fact the best way to do it. Otherwise could anyone point me in the right direction in how to solve this :)

Thanks guys!

unity issue.jpg (28.8 kB)
Comment
Add comment · Show 3
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 Scribe · Mar 31, 2013 at 03:53 PM 1
Share

does the thumbstick produce a vector2 value? something like (Xaxis, Yaxis). I'm unfamiliar with using an xbox controller in unity and can't test it myself

Scribe

EDIT: also do you want the box to move to the angle of the thumbstick and stop, or do you want the object to rotate around the player with the speed of rotation based on the position of the thumbstick...

Sorry if that doesn't make sense!

avatar image whydoidoit · Mar 31, 2013 at 04:24 PM 0
Share

Rotate around gives you an incremental rotation of an object around a point - so by writing a function you could use an absolute rotation - but that involves specifying the initial direction to the point that is then used to produce the rotation.

Rotate around 20 degrees * Time.deltaTime called every frame produces an orbit of 20 degrees per second.

There might be two steps here:

  • Get the rotation and position you want

  • Smoothly rotate and position the item to get there

avatar image robertbu · Mar 31, 2013 at 04:59 PM 0
Share

An alternative solution to using RotateAround(): Place an empty game object at the pivot point. If it is related to the player position (as it appears), make this empty game object a child of the player. When you want to rotate your visible object, make the visible object a child of the the empty game object and do a Transform.Rotate() of the empty game object. When the player lets go of the visible game oject, unparent the game object.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Scribe · Mar 31, 2013 at 11:45 PM

Try this out:

 private var Target = Vector3(0, 1, 0);
 var Player : Transform;
 private var RThumbInput : Vector3;
 var OrbitDist : float = 5.0;
 var Damping : float = 0.5;
 
 function Update () {
     
     RThumbInput = Vector3(Input.GetAxis("360_RThumbButton_X"), Input.GetAxis("360_RThumbButton_Y"), 0);
     
     if(RThumbInput.normalized != Vector3.zero){
         Target = RThumbInput.normalized * OrbitDist;
     }else{
         Target = Target.normalized * OrbitDist;
     }
     
     
     transform.position = Vector3.Slerp((transform.position - Player.position), (Target - Player.position), RThumbInput.magnitude * Damping);
     transform.position += Player.position;
 }

I've used what I'm guessing are your axis names from your script. I can't test it very easily as I've said as I don't have a controller but I'm hoping it will do what you want.

Notes:

  • You need to set the player variable transform to either your player or something else at the centre of the desired rotation.

  • The 'OrbitDist' should be set to how far away you want the object to be held.

  • Damping will need small adjustments your end to make the rotation smooth but not too slow etc.

Good luck, I hope that works for you!

Scribe

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 Scribe · Apr 01, 2013 at 02:47 PM 0
Share

I realised that my code is written to work when attached to the object you are moving rather than the player.

You can fix this quite easily though by changing all instances of 'Player' to transform and creating a new variable for the Object

Sorry for the slight inconvenience!

Scribe

avatar image
0

Answer by Benjam · Mar 31, 2013 at 08:44 PM

Scribe: The joysticks are based on 2 axis, X and Y like you said. They range from -1 to 1 (-1 being left on X and 1 being right on X) the same goes for the Y axis except its up and down. I want the box to rotate around the player smoothly and be in the position of the thumbstick. Which also leads to another problem i am having which is clamping the thumbstick to an outer circular motion.

whydoit and robertbu, i have scrapped rotate around and figured out how to use SLERP. It kind of works with the joystick but as i said before i can't clamp it to the outer circle at the minute. The code i have for it is which is a total re-write of the above code:

 void controllerArc()
 {        
     //rThumbArc = diameter *  PI * (Input.GetAxis("360_RThumbButton_X") + Input.GetAxis("360_RThumbButton_Y")/360);
     
     //right thumbstick arc NEED TO MAKE THIS WORK
     rThumbArc = Input.GetAxis("360_RThumbButton_X") - Input.GetAxis("360_RThumbButton_Y");
     
     //sets up 3 vectors the start position and the end position for the interpolation
     //and the center point 
     Vector3 startPos = transform.position + new Vector3(0,0,5);
     Vector3 endPos = transform.position + new Vector3(0,0,-5);
     Vector3 centre = transform.position;
             
     centre -= Vector3.up;
     
     Vector3 startRelCentre = startPos - centre;
     Vector3 endRelCentre = endPos - centre;
     
     //Vector3 currentPos = Vector3.Slerp(startPos, endPos, rThumbArc);
     Vector3 currentPos = Vector3.Slerp(startRelCentre, endRelCentre, rThumbArc);
     
     if(Input.GetAxis("360_Trigger") < -0.001 && isClose == true)
     {            
         target.transform.position = currentPos;
         target.transform.position += centre;
         
         //for debug purposes
         print(rThumbArc);
     }
 }


The problem with it at the minute is the cube rotates very wildly around the player and does not conform to the outer circle, but instead moves at any point while wiggling the stick around

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

Answer by bmackenzie · May 17, 2013 at 06:19 PM

Forgive the simplicity, but if your simply looking to clamp the movement to an arc and use the thumb stick to control the movement of the object along the arc, would the code below suffice?

 shield = (GameObject)GameObject.Instantiate(shield, transform.position + shieldDistance, Quaternion.identity);
 shield.transform.RotateAround(transform.position, new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal")), rotationSpeed * Time.deltaTime);

I think you could use this as a basis from which to build more complex behavior. If this is way off just pretend I never posted this :)

BAM

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

14 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

Related Questions

Moving player in an arc, from startPoint to endPoint 2 Answers

Xbox 360 Controller (on Mac): Trigger initialises at "0" at game startup, but trigger itself is at position "-1", how can I get the correct value at startup? 1 Answer

XInput for controller/gamepad rumble breaks builds (works fine in editor) 0 Answers

doing force and spin on a gameobject when clicking on plane. 2 Answers

Smooth rotation problem 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