- Home /
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);
}
}
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?
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
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
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!
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?
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?
I want to go further into things if you want, just tell me exactly what you dont understand :)
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.
Your answer
Follow this Question
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