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 Gigabowzer · Feb 19, 2012 at 09:39 PM · fpscontrollerdirectionhowto

Making the gun model point to the middle screen

So, I have so far made something where the gun is able to follow the player, and even go in the direction of the player.

However, I am having trouble having the gun point in the player's camera direction in the y access.

How would I go about doing this? I think I know it requires scripting, which I am ready for.

Here is the game I made just in case you are wondering: http://awsomisoft.com/WebPlayer.html

Comment
Add comment · Show 3
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 asafsitner · Feb 19, 2012 at 10:00 PM 0
Share

`ScreenPointToRay` is your friend here.

Something like this should do:

 RaycastHit screenRayInfo;
 Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width/2f, Screen.height/2f, 0)), out screenRayInfo);
 transform.LookAt(screenRayInfo.point);

avatar image Gigabowzer · Feb 19, 2012 at 10:33 PM 0
Share

I tried that, but now the gun does not turn with the player.

Is this what the code should look like:

using UnityEngine; using System.Collections;

public class FollowCamera : $$anonymous$$onoBehaviour {

 void Update ()
 {    
  RaycastHit screenRayInfo;
  Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width/2f, Screen.height/2f, 0)), out screenRayInfo);
  transform.LookAt(screenRayInfo.point);
 }
 
 void Start ()
 {
     
 }

}

avatar image Kacer · Feb 20, 2012 at 11:33 AM 0
Share

I dont know if its because im using chrome, but there seems to be some issues with looking around, the controls are locking up as well.

looking good so far.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Gigabowzer · Feb 20, 2012 at 03:23 AM

Well, I tried that, but now the gun does not even go in the direction of the player anymore.

Here is the code now:

 using UnityEngine;

using System.Collections;

public class FollowCamera : MonoBehaviour {

 void Update ()
 {    
  RaycastHit screenRayInfo;
  Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3(Screen.width/2f, Screen.height/2f, 0)), out screenRayInfo);
  transform.LookAt(screenRayInfo.point);
 }
 
 void Start ()
 {
     
 }

}

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 Berenger · Feb 21, 2012 at 03:54 AM

Actually, you don't need the gun to follow the player at all. You can renderer it with an other camera which will render only the gun's layer, when the main camera, the one that follow your player, will render everything but. The same goes for the players hands, if you want any.

The gun camera mustn't have an AudioListener, must have it's clear flags to depth only, and the culling mask to you're weapon's layer only.

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

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

8 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Jumping and maintaining current velocity 1 Answer

Unity3D HELP!!!!!!!!!!!!!!!!!!!!!! : How can i make physics controller jump/PLAY SOUND. 1 Answer

Unity3D HELP! How can i pickup/hold/throw object when im rolling ball? 4 Answers

UFPS:ultimate fps cameraGUI texture wont show in game 0 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