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 benarceneaux · Aug 22, 2013 at 04:17 PM · move an objectlocal axis

Why are my projectiles not following Input.pos after transform movement?

Hey guys, this is pretty specific, I tried searching but couldn't find anything that matched my problem exactly. Here's a link to a youtube video of my game playing and you'll see that I first shoot a couple of times before moving my player object. The projectiles follow the code which looks like this:

Youtube link: http://www.youtube.com/watch?v=5GTwCXNXof8&feature=youtu.be (Ignore the music, the screen capture program automatically attaches it and I don't know how to remove it)

Instantiate Bullet Prefab

LookAt MousePosition

Velocity = tranform.forward * shotSpeed(public variable)

This works out fine. I have a disabled camera that is a child of my player object that I use to do the raycast and a plane that is at 0y while my disabled camera is at 2y. (It's 2D as the video shows so I'm looking down the y-axis and tranform.forward, the z-axis is my up/down, x-axis is my left/right)

However, as you can see in the video, after I move the player object in any direction, something breaksdown and I'm no longer getting an accurate shot. It seems to be random even and sometimes won't work if I try to shoot south of the player object.

I'm really not sure what's happening here, it seems like it must be something small that I'm just not aware of being a problem because it works fine before I move the player transform... Thanks in advance to anyone who answers, and also thanks for even looking at my problem!

This is the script attached to the prefab bullet. It is instantiated when the user clicks the mouse button: void Start () {

     playerCam = GameObject.FindGameObjectWithTag("PlayerCamera").camera;
     
         
     RaycastHit hit;
     
     Ray    ray = playerCam.ScreenPointToRay(Input.mousePosition);
     
     if (Physics.Raycast(ray, out hit))
     {
         gameObject.transform.LookAt(hit.point);
         Debug.Log ("hit point " + hit.point);
         rigidbody.velocity = transform.forward * shotSpeed;

     }
 }
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 benarceneaux · Aug 23, 2013 at 12:50 AM

Okay so I ended up fixing this issue myself by trying out a few things. The answer was inspired by something robertu mentioned although I didn't change anything except for which camera I used in ScreenPointToRay. I'd mark your post as the answer robertu except that I'd worry people would get confused by my off the cuff posting. Like you mentioned, I did not need the playerCam, I just used the main topdown camera which is stationary and the aiming remains on target after moving the transform.

However, I now have noticed a new issue where the closer the aiming reticle is to the player object, when the fire button is clicked, it shoots slower. So, the closer to the player, the slower the projectile's velocity. The velocity is only set once, in the script attached to the instantiated bullet. Probably is something to do with the rigidbody/physx that I'm not familiar with.

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 robertbu · Aug 22, 2013 at 04:38 PM

You issue is the playerCam. Your code should work if you use the main camera to create the ray rather than the playerCam. Taking the issue one step further, you don't need a plane and Physics.Raycast() to find the aim point. You can use Camera.ScreenToWorldPoint(). So assuming your main camera is the one tagged 'MainCamera', you can do the following:

 Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y);
 pos = Camera.main.ScreenToWorldPoint(pos);
 gameObject.transform.LookAt(pos);
 rigidbody.velocity = transform.forward * shotSpeed;

This code assumes your playing plane is at 0 on the 'y' axis.

Comment
Add comment · Show 6 · 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 benarceneaux · Aug 22, 2013 at 05:19 PM 0
Share

I'll give it a try. I had it so that it used the main camera before but I kept ending up with rotation issues. The only part of your code that I am not familiar with is this:

-Camera.main.transform.position.y);

What does this do exactly?

avatar image benarceneaux · Aug 22, 2013 at 05:56 PM 0
Share

This didn't work. Something might be off with my local/global axis because the bullets are being instantiated in way off places. I'm not sure how to get everything back in the same local/global axis. It's like some objects have different transforms. What I mean is that when I click on one object, it's orientated how it should be, y is in/out, z is up/down, and x left/right. However, when I click on others in the editor they go to a traditional non-topdown view as in y is up/down and z is in/out.

avatar image robertbu · Aug 22, 2013 at 06:11 PM 0
Share

I had an error in the code above which I fixed. Put the following on an empty game object. It will place a sphere at the point clicked to verify the position logic:

 using UnityEngine;
 using System.Collections;
 
 public class Clicker : $$anonymous$$onoBehaviour {
 
     void Update () {
         
         if (Input.Get$$anonymous$$ouseButtonDown(0)) {
             Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.transform.position.y);
             pos = Camera.main.ScreenToWorldPoint(pos);
             GameObject go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             go.transform.position = pos;
             go.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
         }
     
     }
 }
avatar image benarceneaux · Aug 22, 2013 at 06:31 PM 0
Share

Okay, I'm getting a few errors in the log and this seems to be the culprit:

Camera.main.transform.position.y

The intellisense(I'm at a loss for it's name at the moment) isn't recognizing Camera.main.transform.y as a valid input.

Assets/testLocal.cs(19,113): error CS1503: Argument #3' cannot convert object' expression to type `float'

EDIT

I fixed it by making another vector3 variable and assigning it the main camera's transform. The sphere instantiates on the correct axis. z = up/down and y = in/out

Something else...

I added a lookAt to the script and had it target a Transform object. If I have it lookAt the reticle object I created the z axis points towards the camera, which is not correct so something is wrong there but having it look at Input.mousePosition also does not work. If I do that, the sphere's all instantiate at the same, but correct, position and axis

avatar image robertbu · Aug 22, 2013 at 07:20 PM 0
Share

I'm having trouble understanding what is going on. Can you post your current ai$$anonymous$$g code?

Show more comments

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

15 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

Related Questions

How to move an object a fixed distance along one of its axis 1 Answer

Bike/Motobike game 1 Answer

Applying a force to one objects rigidbody on another objects local axis 0 Answers

Moving forward in 3D 1 Answer

How does GUI TextField Work? 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