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 bschubert · Oct 31, 2017 at 02:59 PM · transform2d gameposition2d spritesfacing

2D RPG boomerang instantiation help.

ive been working on this for 2 days now and i can't seem to figure out how to code the direction my player is facing in an open 2d rpg style world. what im trying to do is instantiate a boomerang that flings out from the player depending on the direction he is facing. if i could call the name of an animation that is facing a certain way i could code in a vector3.up if the animation is up or a vector3.down ect. but from what i understand from the past two days of research is that you can't call the name of an animation from the blend tree. if i was in a 3d environment i would just use transform.forward but all that does is throw through my scene on the z axis. im kinda at a loss on how to figure out which way my 2d sprite is facing.

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

2 Replies

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

Answer by bschubert · Nov 01, 2017 at 12:20 AM

@MacDx my environment is setup for android and im using the mobile control joystick for movement. i wrote the scripts for the boomerang and the instatiation button but im not sure how to interpret everything under the joystick script.

namespace UnityStandardAssets.CrossPlatformInput { public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler { public enum AxisOption { // Options for which axes to use Both, // Use both OnlyHorizontal, // Only horizontal OnlyVertical // Only vertical }

     public int MovementRange = 100;
     public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
     public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
     public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input

     Vector3 m_StartPos;
     bool m_UseX; // Toggle for using the x axis
     bool m_UseY; // Toggle for using the Y axis
     CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis; // Reference to the joystick in the cross platform input
     CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis; // Reference to the joystick in the cross platform input

     void Start() 
     {
         CreateVirtualAxes();
     }

     void OnEnable()
     {
         m_StartPos = transform.position;
     }

     void UpdateVirtualAxes(Vector3 value)
     {
         var delta = m_StartPos - value;
         delta.y = -delta.y;
         delta /= MovementRange;
         if (m_UseX)
         {
             m_HorizontalVirtualAxis.Update(-delta.x);
         }

         if (m_UseY)
         {
             m_VerticalVirtualAxis.Update(delta.y);
         }
     }

     void CreateVirtualAxes()
     {
         // set axes to use
         m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
         m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);

         // create new axes based on axes to use
         if (m_UseX)
         {
             m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
             CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
         }
         if (m_UseY)
         {
             m_VerticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
             CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
         }
     }


     public void OnDrag(PointerEventData data)
     {
         Vector3 newPos = Vector3.zero;

         if (m_UseX)
         {
             int delta = (int)(data.position.x - m_StartPos.x);
             //delta = Mathf.Clamp(delta, - MovementRange, MovementRange); bschubert
             newPos.x = delta;
         }

         if (m_UseY)
         {
             int delta = (int)(data.position.y - m_StartPos.y);
             //delta = Mathf.Clamp(delta, -MovementRange, MovementRange); bschubert
             newPos.y = delta;
         }
         transform.position = Vector3.ClampMagnitude( new Vector3(newPos.x, newPos.y, newPos.z),MovementRange) + m_StartPos;
         UpdateVirtualAxes(transform.position);
     }


     public void OnPointerUp(PointerEventData data)
     {
         transform.position = m_StartPos;
         UpdateVirtualAxes(m_StartPos);
     }


     public void OnPointerDown(PointerEventData data) { }

     void OnDisable()
     {
         // remove the joysticks from the cross platform input
         if (m_UseX)
         {
             m_HorizontalVirtualAxis.Remove();
         }
         if (m_UseY)
         {
             m_VerticalVirtualAxis.Remove();
         }
     }
 }

}

and this is my boomerang script

private Transform playerTransform; bool returning=false; //float boomerangTimer; public float speed; float boomerangDistance;

 // Use this for initialization
 void OnEnable() 
 {
     playerTransform = GameObject.Find ("Player").transform;

     
 }
 
 // Update is called once per frame
 void Update () 
 {
     boomerangDistance =Vector3.Distance(playerTransform.position, transform.position);
     if (boomerangDistance >= 1.5f) 
     {
         returning = true;
     }

     if (!returning) {


             
             transform.Translate (Vector3.forward * speed * Time.deltaTime);

             
     }

     else
     {
         transform.LookAt(playerTransform.position);
         transform.Translate (Vector3.forward * speed * Time.deltaTime);
     }

 }
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.tag == "Player" && returning==true) 
     {
         Destroy (this.gameObject);
     }
 }

if i understand what your saying if i put a simaliar code that you showed as nested ifs un the !returning if it should instantiate correctly? only thing is im not sure what i would use as the if's arguments the record what way the joystick is moving.

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 MacDx · Oct 31, 2017 at 09:13 PM

Hi. If I understand your problem correctly you are basically trying to throw a projectile towards the direction your player is facing, however you aren't actually doing any rotation to your player object so the transform's forward will basically always be the same.

What you need to do is come up with your own direction value. What does facing direction means in your game? Can your player only move, up, down, left, and right? Or is it more of a free way of moving?

In any case what you need to do is store the direction you player is MOVING towards. Moving is the word that is key here. The direction you are moving towards is also the direction you are facing (I'm assuming) so you need to use that same direction to throw the boomerang.

I'll give you a small example. Let's say this is the player script.

 public Vector3 direction;

 void Update()
 {
     direction = Vector3.zero;
     if (Input.GetKey(KeyCode.UpArrow))
     {
         direction.y = 0.1f;
     }
     if (Input.GetKey(KeyCode.DownArrow))
     {
         direction.y = -0.1f;
     }
     if (Input.GetKey(KeyCode.LeftArrow))
     {
         direction.x = -0.1f;
     }
     if (Input.GetKey(KeyCode.RightArrow))
     {
         direction.x = 0.1f;
     }
     transform.position += direction;
 }

Do you see? The variable direction which I'm using to move is the same direction I'm facing so I would use that to throw the boomerang. Now this example is using the keyboard for movement but that actually doesn't matter. Your movement code needs to make your player moves toward a certain direction and that same direction is the one you use as a forward for throwing your boomerang.

Hope this helps! Don't hesitate to ask if you have any questions!

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

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

Related Questions

Adding a value to transform.position.y runs very untrusted 0 Answers

How to make object follow y-axis of another object. 1 Answer

Unity3D - Playback object array of position (with dynamic velocity) 0 Answers

How to Position the object perfectly with the Screen Bounds 0 Answers

How to decide an object's position when using Instantiate? 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