- Home /
Make shift "gun" points down when falling or jumping
So my make shift "gun" keeps looking down when you fall, it's really weird. The gun has 4 parts: the invisible bullet spawnpoint (sphere),the tip of the gun (sphere), the body of the gun (cylinder), and all those parts are children to an empty GameObject which is positioned in the center of the cube, and is a child of the player (basic unity cube). That way it all rotates around my player's body. My problem can be seen here: http://youtu.be/2-7gKChaPFo if you need visual help understanding what I mean. When I jump, the gun moves up and down, which I'm fine with, but when I fall off the edge, the gun looks straight down. I have the gun looking at the mouse cursor at all times (this is attached to every part of the gun, including the empty GameObject):
function Update () {
transform.LookAt(PlayerScript.hitPoint); //get mouse position (hitPoint) from my PlayerScript
transform.Rotate(0,-90,0); }
To find the mouse position, I use the following code attached to my playerscript:
// this creates a horizontal plane passing through this object's center
var plane = Plane(transform.position, Vector3.up);
// create a ray from the mousePosition
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// plane.Raycast returns the distance from the ray start to the hit point
var distance: float;
if (plane.Raycast(ray, distance)){
// some point of the plane was hit - get its coordinates
hitPoint = ray.GetPoint(distance);
hitPoint.z = -19; // always -19 because of no movement on z axis
}
So any help would be greatly appreciated. Once again, I'm trying to solve the problem with the "gun" not looking at the mouse when falling, I want it to always look at the mouse. Also as a bonus question, if someone can solve the problem of my charcter not smoothly falling (see 10-14 seconds on the video) that would be great, but I don't expect that to be solved cause it's not the main problem in the question. Just incase you know the answer or think you do, here's my code for jumping:
if (controller.isGrounded) {
jump = false;
vertVel = 0;
if (Input.GetButton ("Jump") && jump == false) {
moveDirection.y = jumpSpeed;
vertVel = jumpSpeed;
jump = true;
}
}
// Apply gravity
vertVel -= gravity * Time.deltaTime; // apply gravity to the vertical velocity
moveDirection.y = vertVel;
thank you for any help which may come from this, you guys are an amazing community!
*All code provide is in the Update function
can anyone help? It's really annoying me. Also, the youtube video is just 30 seconds long, please take a look, you might know how to fix it.
Answer by aldonaletto · May 25, 2012 at 11:49 AM
I don't know if this is the main cause, but the plane generation is wrong:
// this creates a horizontal plane passing through this object's center
var plane = Plane(transform.position, Vector3.up);
The arguments for Plane are reversed - they should be:
// this creates a horizontal plane passing through this object's center
var plane = Plane(Vector3.up, transform.position);
In the original code, the plane would rotate as the player moves, producing weird results depending on the player position relative to 0,0,0 (I hate to say this, but this wrong code seems to have been extracted from an older answer of mine...)
When I change it around, the barrel no longer follows the mouse on the x and y axis, but now seems to only work on z and x. By this I mean it only works horizontally, and not vertically at all. It also gives me an error on this line of code: direction = hitPoint - transform.position;
the error is: NullReferenceException: Object reference not set to an instance of an object
On the bright side, the barrel doesn't fall down when I jump.
EDIT: Also when I jump, the gun starts rotating downward and then pops back up to be horizontal. This does not happen when I fall however.
Ok, the screen in your game shows the XY plane: change the plane normal to -Z:
var plane = Plane(-Vector3.forward, transform.position);
This will create a plane parallel to the screen, thus any click will find a valid hit point.
That seems to have worked. Thank you once again for all of your help with my many problems.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071913im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I'm having trouble with my character when playing in unity, and when I build and run the game 0 Answers
Need help with screen resolution and character size 1 Answer
When I move my arms back and forth, the player stops falling or moves upward 2 Answers
A node in a childnode? 1 Answer
Adding a Sprite 1 Answer