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 MrCottonmann · Apr 22, 2014 at 04:38 AM · cameraplayercursor

Camera watching inbetween the player and cursor

Hello everyone!

I have a question regarding player camera/controls, which involves the camera to look at the middle point between the player and the cursor like this: -----X----- and how to make the player look at the cursor but when moving to ignore moving towards the cursor.

So my 2 Questions are this: What can I put into my code to make it so my camera focus' in between my player and the cursor and show a prefab at the cursor location?

How can I make the player look at the cursor but ignore moving towards it?

Here is my code for moving:

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CharacterController))]
 public class Controller:MonoBehaviour
 
 {
     public float MoveSpeed;
     public float RotationSpeed;
     CharacterController cc;
 
     void Start()
     {
         cc=GetComponent<CharacterController>();
     }
 
     void Update()
     {
         Vector3 forward = Input.GetAxis ("Vertical") * transform.TransformDirection (Vector3.forward) * MoveSpeed;
         transform.Rotate(new Vector3(0,Input.GetAxis("Horizontal") * RotationSpeed * Time.deltaTime,0));
         cc.Move (forward * Time.deltaTime);
         cc.SimpleMove(Physics.gravity);
     }
 }
 
Comment
Add comment · Show 2
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 getyour411 · Apr 22, 2014 at 05:30 AM 0
Share

First or Third Person? Also, the combo use of cc.$$anonymous$$ove and cc.Simple$$anonymous$$ove isn't something I've seen before - you need to do that?

avatar image MrCottonmann · Apr 22, 2014 at 05:46 AM 0
Share

Its 3rd person, and apart of the script that you mentioned I used from this video: https://www.youtube.com/watch?v=vPO$$anonymous$$Z62SAiI

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NickP_2 · Apr 22, 2014 at 06:38 AM

To get the middlepoint of vectors, you need to add each vector position and then devide by the amount of vector positions, for you this would be Vector3 midPoint = (player.transform.position + mouseWorldPos) / 2;

For the mouse position in world space, take a look [here][1].

For something to look at a certain point, unity has a nice build-in function:

 Transform.lookAt(<position>);

Make sure the forward of the object is correct, you can always change that.

Now for the charactercontroller to do a motion so it wont move towards the camera, you could give the motion a spin of lets say 90 degrees before executing it (just an idea I hope im not wrong).

To do so, use:

 Quaternion.euler(new Vector3(0, 90, 0)) * <your motion(vector3)>;

Make sure to put the quaternion on the left side of the "times * thing", if you switch both the quaternion and vector3 from side itll trow an error.

Sorry for the not so perfect english. I also didnt check for compile errors since im on my phone. Anyways, I hope i helped you somewhat! [1]: http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

Comment
Add comment · Show 6 · 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 NickP_2 · Apr 22, 2014 at 06:44 AM 0
Share

Also get rid of the simplemove, you can merge the gravity calculation in the move function. You can see an example in unity docs from charactercontroller! Good luck!

avatar image MrCottonmann · Apr 22, 2014 at 07:50 AM 0
Share

Hey thanks for the help! Im still kinda new to coding and such and I am not sure were to impliment this code and I will take a look at the link you sent.

----EDIT---- I think some of the errors I am getting from trying to impliment this code is that the code you provided is for JS and not CS# (unless if I am doing something wrong). How could I convert the code I have to JS?

avatar image NickP_2 · Apr 22, 2014 at 10:01 AM 0
Share

It should be c#, and its ok to be new, always good to have new peeps! But i wont give you the code directly, im the asshole who wants you to learn something ;)

Are you new to .net or c# or any other coding language, or just to the unity engine?

EDIT

the code i gave you are examples and wont work without changing a few things. For example: you need to define "player" as a public variable and link the gameobject in the inspector of the script in order for it to execute, i hope you already know how to do this?

avatar image NickP_2 · Apr 22, 2014 at 10:06 AM 0
Share

I want to go further into things if you want, just tell me exactly what you dont understand :)

avatar image MrCottonmann · Apr 22, 2014 at 06:59 PM 0
Share

haha yea I would like to learn whatever I can and as far as me being new to the different languages, everything is sorta of a mystery as I kinda know some coding in JS but not enough to whip out a script to run but for C# I dont know that much. For defining the "player" I tried public class player; but that didnt work.

Show more comments

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera Tracks player not in the middle 1 Answer

Zoom camera based on position between player an object 2 Answers

3rd person player 0 Answers

Multiplayer Cameras First Person One Camera 0 Answers

Change Player MiniMap Icon 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