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 JCross_99 · Feb 01, 2017 at 06:36 AM · rotationrigidbody

Top-down (3D) rotate arrow projectile towards shoot position

Hello there, I'm working on a top-down game in which a player can shoot an arrow projectile which moves in the direction of the mouse. I've set the arrow object to have a rigidbody and rotated the object to be facing forward (pic A).

I'm attempting to rotate the projectile along it's Y-axis (as that's the one I have setup to rotate around the players movement directions) so the arrow will point towards the movement direction rather than how it is right now (pic B).

Code:

 using UnityEngine;
 using System.Collections;
 
 public class Player: MonoBehaviour {
     public GameObject arrowProjectile;
 
     void Start()
     {
 
     }
 
     void Update()
     {
         if (Input.GetButtonDown("Ability_1"))
         {
             Vector3 myPos = transform.position;
             float dist = Camera.main.transform.position.y - myPos.y;
             float x = Input.mousePosition.x;
             float y = Input.mousePosition.y;
             Vector3 dir = Camera.main.ScreenToWorldPoint(new Vector3(x, y, dist)) - myPos;
             GameObject projectile = (GameObject) Instantiate(arrowProjectile);
            
             Rigidbody rigidbody = projectile.GetComponent<Rigidbody>();
             rigidbody.velocity = dir.normalized * 5.0f;
 
             Vector3.RotateTowards(projectile.transform.forward, dir, 5.0f * Time.deltaTime, 0.0f);
         }
     }
 }

Pic A: alt text

Pic B: alt text

example.png (184.0 kB)
example.png (233.2 kB)
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 NorthStar79 · Feb 01, 2017 at 07:45 AM

take a look at "transform.TransformDirection()" and "Transform.TransformPoint" and also Transform.InverseTransformDirection . you can find further details at unity script refence docs.

Comment
Add comment · Show 8 · 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 JCross_99 · Feb 01, 2017 at 07:57 AM 0
Share

I've read over those docs before (and I did again) and I do not see anything talking about rotation of an object's transform. I have an idea of the concept of getting the position of the mouse, and using that to modify the projectile's Y rotation from where it starts using some sort of math formula but I'm not sure exactly what that formula would be.

avatar image NorthStar79 JCross_99 · Feb 01, 2017 at 08:04 AM 0
Share

well , just try to raycast from your mouse position , and get world x and z position that raycast hits. and use transform.LookAt for arrow rotation. this should be solve your problem . if i understand you correctly , if you couldn't get it work. let me know. wil try to add an example script on my free time

avatar image JCross_99 NorthStar79 · Feb 01, 2017 at 08:13 AM 0
Share

Honestly an example would be super helpful. I'm going to continue to try to figure this out for the next 20 $$anonymous$$utes or so then head to bed. $$anonymous$$aybe I should set all transform values to 0 (position / rotation) in the prefab to make it less confusing?

Show more comments
Show more comments
avatar image NorthStar79 · Feb 01, 2017 at 09:45 AM 0
Share

idk. that should work , just keep tweaking numbers , and also pay attention the "5" over here arrow.transform.LookAt(new Vector3(hit.point.x,5,hit.point.z)); you may want to tweak this number too.

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

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

Related Questions

Torque rigidbody toward desired rotation on two axes ignoring Y axes? 0 Answers

How to make a cube rotate while moving? 1 Answer

How do I get a Rigidbody to rotate in the direction it is moving? 0 Answers

Question about NavMesh and Rigidbody 0 Answers

Rotate rigid body with mobile joystick 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