• 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 timo0060 · May 24, 2012 at 08:42 PM · 2dfallingsidescroller

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

Comment
Add comment · Show 1
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 timo0060 · May 25, 2012 at 01:55 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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...)

Comment
Add comment · Show 3 · 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 timo0060 · May 25, 2012 at 12:53 PM 0
Share

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.

avatar image aldonaletto · May 25, 2012 at 02:23 PM 1
Share

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.

avatar image timo0060 · May 25, 2012 at 02:35 PM 0
Share

That seems to have worked. Thank you once again for all of your help with my many problems.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

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


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