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 Borzi · Apr 14, 2013 at 04:30 PM · multiplayerfpscharactercontrollermodelmouselook

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?

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 Loius · Apr 14, 2013 at 04:45 PM 0
Share

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.

avatar image Lairinus · Apr 14, 2013 at 05:01 PM 1
Share

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.

avatar image Borzi · Apr 14, 2013 at 07:43 PM 0
Share

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!

1 Reply

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

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.

Transform.translate()

Hope I am clear enough to steer you in the right direction.

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

14 People are following this question.

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

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


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