- Home /
Making a Model move with the Mouse
Ive started using Unity recently and its been a blast, but now I encountered a small problem and was wondering if one of you could help me. Ive been working on a first person multiplayer game and been using a piece of script to tie (make child of) the main Camera to an object floating above the character controller instead of the normal first person one so that the game only requires one main camera in total. This object has mouse look attached to it. Its been working great, but now I tried to add a model in front of my character as a gun and attached it to the camera object, but I got some weird results:
When looking Up:
http://s14.directupload.net/file/d/3225/vg6b9v9m_png.htm
When looking straight (how I want the model to look at all times):
http://s7.directupload.net/file/d/3225/f5ff5rbp_png.htm
When looking down:
hhttp://s7.directupload.net/file/d/3225/v8wedjij_png.htm
As you can see, the model looks like its falling apart. In addition, as you might have noticed when the player looks up the model goes further away while it goes into the player when I look down. I cant fix the weapon falling apart with the "Combine Children" skript, but I think that I need the model to move along with the mouse so that it works properly. Does anyone have any tips on what I can do, or know how to script such a command?
What script are you using? You should be able to just parent the weapon to an object and the object will magically exert complete control over it.
The reason I feel this is happening is because you have the Camera at a certain height and rotation, and then the model at a different height and rotation without the Camera actually looking at the model.
I encountered situations like especially in rotation scripts where I would have a "camera look at" object, and when I would rotate the camera around the character, I would be too far from him on one side, and literally clipping through him on the other.
The best way to do this (well, only way that I know how, but that's not saying much) would be for you to make an empty game object child and attach that to the camera. Then directly "childing" the model to the empty GameObject should keep the rotation and orientation the same.
Basically, you want the center of the screen to have the empty object , and then the model will be a child of the game object, but offset as you like. That way while you're looking at the center of the screen, you will keep the gun's rotation the same.
using UnityEngine; using System.Collections;
/// /// This scripts is attached to the player and it /// causes the camera to continously follow the /// CameraHead object. ///
public class CameraFirstPerson : $$anonymous$$onoBehaviour {
//Variable Start
private Camera myCamera;
private Transform cameraHeadTransform;
//Variables End
// Use this for initialization
void Start () {
myCamera = Camera.main;
cameraHeadTransform = transform.FindChild("CameraHead");
}
// Update is called once per frame
//Void Update makes the Camera follow the player's cameraHeadTransform
void Update () {
myCamera.transform.position = cameraHeadTransform.position;
myCamera.transform.rotation = cameraHeadTransform.rotation;
}
}
This is the script I've been using for the camera to attach it to the "cameraHead", or the empty object. I got the idea from a tutorial:
http://www.youtube.com/watch?v=nx82$$anonymous$$PPasOg
At 26:00 he explains the camera head. The problem is when I try to parent my musket to the Camera Head and want to scale the head to 0, 0, 0...the musket also gets really small. I thought this would solve the problem but nope :/ Im guessing that Im gonna need some kind of script on the musket because nothing seems to work. To Shingox: Thanks for the help, I'll try and adjust the changes just gotta fix somethign else first!
Answer by 1337GameDev · Apr 14, 2013 at 09:46 PM
Just raycast the mouseposition to a plane (with a collider of course) and use that hit position (after screen out other objects and making sure the plane is hit) and then use translate to move the object. To find the direction to translate, take (targetPosition - objectPosition) and feed this to the translate function.
Hope I am clear enough to steer you in the right direction.
Your answer
Follow this Question
Related Questions
Vertical axis not working properly 0 Answers
Why is my camera rotating like crazy? 2 Answers
How to handle movement in a multiplayer FPS? 0 Answers
Check if Transform Tags are Identical 1 Answer
Multiplayer FPS Character Model Question 3 Answers