- Home /
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
`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);
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 ()
{
}
}
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.
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 ()
{
}
}
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.
Your answer
Follow this Question
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