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 /
avatar image
0
Question by Alan Wyvern · Aug 26, 2015 at 12:47 PM · rotationanglecontrol

Rotate object using analogue stick around player object

Ok, I've tried a bunch of things and nothing is ever remotely working. I was hoping one of you fine people would be abel to point me in the right direction.

I have the player object which moves around using the left analogue stick, great! What I want is a shield object that is directed around the player completely separate from the player (I have another game object for this) a set distance from the player pointing the direction of the right analogue stick.

I do have the right analogue stick set up and working (tested) but for the life of me and after lots of trial and just as much error (total noob) I still don't know how I'd go about this

help? please? you're awesome!

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

Answer by Barachiel · Aug 26, 2015 at 07:11 PM

I would suppose the easiest way to go about this would be to have an empty GameObject, with a script on it to follow the player character. Place your shield as a child of this, moving it out to the distance from the player that you want.

From there, in the script you now have on it's parent (the one to keep it at the same location as the player), rotate the object based on the input of the right stick.

The result should be that the empty GameObject will rotate while staying centered on the player, and the shield will rotate with it, staying at the distance you placed it at.

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 Alan Wyvern · Aug 27, 2015 at 10:47 PM 0
Share

Thanks @Barachiel

I've got this so far:

public class Shield$$anonymous$$ovement : $$anonymous$$onoBehaviour {

//public float OrbitSpeed = 45.0f; //public float speed = 10.0f;

//public Transform pivot; float distance = 0.8f; Vector3 direction = Vector3.up; float speed = 1.0f; public Transform target;

// Use this for initialization void Start () {

}

// Update is called once per frame void Update () {

 if(Input.GetAxis("rightH") != 0f || Input.GetAxis("rightV") != 0f)
 {
     direction =  new Vector3(Input.GetAxis("rightH")*speed*Time.deltaTime,Input.GetAxis("rightV")*speed*Time.deltaTime, 0.0f ) ;
 }

 Ray ray = new Ray(target.position, direction);


 transform.position = ray.GetPoint(distance);


 float angle = $$anonymous$$athf.Atan2 (Input.GetAxisRaw("rightV"), Input.GetAxisRaw("rightH")); 

 transform.rotation = Quaternion.AngleAxis(angle,Vector3.forward);


}

}

It starts in the right position and moves to the position where the right analogue stick is pointing, however the shield isn't rotating to face away from the player, the rotation stays static.

Also I'd like it to be a smooth rotation of the shield around the player going to a new location ins$$anonymous$$d of teleporting there

Any ideas?

avatar image Barachiel · Aug 28, 2015 at 04:36 AM 0
Share

Okay, so first make sure that the rotation of your empty gameobject is set to 0,0,0.

Then, place the script on it and parent the shield to it in whichever order you like, adjusting the shield, etc.

$$anonymous$$ake sure the shield is at the correct distance and rotation as if it were directly in front of the character.

Then, try something like this in the script on the empty gameobject:

 using UnityEngine;
 using System.Collections;
 
 public class ShieldOrbitTest : $$anonymous$$onoBehaviour {
 
     public Transform target;
     public float rotationSpeed = 4f;
     public float joystickDeadzone = 0.2f;
 
     float xAxis;
     float yAxis;
 
     void Start () {
         
     }
     
     void Update () {
 
         this.transform.position = target.position;
 
         if ($$anonymous$$athf.Abs(Input.GetAxis("Horizontal")) >= joystickDeadzone || $$anonymous$$athf.Abs(Input.GetAxis("Vertical")) >= joystickDeadzone)
         {
             xAxis = Input.GetAxis("Horizontal");
             yAxis = Input.GetAxis("Vertical");
         }
 
         float joystickAngle = $$anonymous$$athf.Atan2(xAxis, yAxis) * $$anonymous$$athf.Rad2Deg;
 
         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0,joystickAngle,0), Time.deltaTime * rotationSpeed);
 
     }
 }

I tested this out and it works fine, so let me know if you have any trouble with it. It's very basic and could be done better, but it's simple, works and can be expanded on.

I don't know how you have your inputs set up, so I'm just using horizontal and vertical here, which means that the wasd keys and the arrow keys will move the shield, as will the joystick, under normal input setups. You can obviously change these as needed.

In the if statement, I just use $$anonymous$$athf.Abs in front of the input check so that we can tell if the joystick is moving at all, since $$anonymous$$athf.Abs just returns a positive value, otherwise we'd need to do two checks, one for a positive value and one for a negative value.

I also added in a deadzone for the input, so that you can easily tweak it if you want high speed turning but feel it's too twitchy.

Also, if you want the shield to always return to being in front of the player when no input is given, you can just remove the if statement.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make camera position relative to a specific target. 1 Answer

Problems caused by non-unique Euler angle solutions 1 Answer

Restricting rotation to multiples of 15? 1 Answer

Convert Object Rotation to X, Y values 1 Answer

How do I apply rotations to parent child using Rigidbody? 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