- Home /
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;
}
}
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.
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.
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?
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.
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);
}
}
}
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
I'm having trouble understanding what is going on. Can you post your current ai$$anonymous$$g code?
Your answer
Follow this Question
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