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
0
Question by FizzyBear · Apr 09, 2012 at 11:20 AM · animationjavascriptgameshooting

[SOLVED] Shooting fail (Due to camera setup)

Hi, so I've set my camera to respond to both the WASD and mousemovement, for everyone that's played Counterstrike, it is of a similar function, if not, then here's a brief explanation W: Forward A: Strafe left S: Back D: Strafe right.

Mouse moves right = Turns right Mouse moves left = Turns left (Both concept similar to steering).

I'll paste the code for the camera if needed.

Anyways - The real question is when I shoot a clone from the spawnpoint (the spawnpoint is set within the elemental staff) it shoots with the WASD, but when I turn with the mouse, it shoots from it's last know location with the WASD. So how do I make my camera & fire understand that when I turn with the mouse, you shoot at what he's looking at rather than shooting backwards :/

 var projectile : Transform;
     var bulletSpeed : float = 20;
     
     function Update () {
         // Put this in your update function
          if (Input.GetMouseButtonDown(0)) {
     
         // Instantiate the projectile at the position and rotation of this transform
         var clone : Transform;
         clone = Instantiate(projectile,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);
     
         // Add force to the cloned object in the object's forward direction
         clone.rigidbody.AddForce(clone.transform.forward * 1000);
         }
      }

UPDATE -

I changed the following line

     clone = Instantiate(projectile,GameObject.Find("spawnPoint").transform.position, Quaternion.identity);

into

  clone = Instantiate(projectile,GameObject.Find("spawnPoint").transform.position, transform.rotation);

Now that allows me to shoot when the mouse movement changes, but upon shooting it doesn't shoot straight anymore!

Anyone got any ideas?

[SOLVED] - I adjusted my spawnPoint, apparently it was upside down.. Rofl. Blah.

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
1
Best Answer

Answer by ExTheSea · Apr 09, 2012 at 12:17 PM

You have to instantiate the clone also at the transform.rotation your looking at. So i think you should ad GameObject.Find("spawnPoint").transform.rotation after the position when spawnpoints points in the direction you want the clone to move in.

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 gregzo · Apr 09, 2012 at 12:07 PM

Adapted from the docs, TransformDirection :

    // Calculate the x-axis relative to the camera
 var cam : Transform = Camera.main.transform;
 var cameraRelativeForward : Vector3 = cam.TransformDirection (Vector3.forward);
 // Apply a force relative to the camera's x-axis
 rigidbody.AddForce (cameraRelativeForward * 1000);
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 FizzyBear · Apr 09, 2012 at 01:22 PM 0
Share

Even applying this technique or trying to make it work around my code. I still get errors, I see what it's trying to do, but in order to use this, I'd have to remove all my code :/

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooting length 1 Answer

Enemy not getting hit (Collider issue) 2 Answers

[SOLVED] Shooting from Elemental Staff 1 Answer

[Solved] Generating Particles with Attack Animation 1 Answer

How to wait until an animation is finished. 4 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