hey i thought would be a good idea to share my script of free third person camera and movement since i had trouble finding some solid and straight forward scripts out there.
i made two scripts one for the camera and other for the player using the character controller component but of corse you can adapt whatever you like.
Camera Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour
{
private const float YMin = -50.0f;
private const float YMax = 50.0f;
public Transform lookAt;
public Transform Player;
public float distance = 10.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
public float sensivity = 4.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
currentX += Input.GetAxis("Mouse X") * sensivity * Time.deltaTime;
currentY += Input.GetAxis("Mouse Y") * sensivity * Time.deltaTime;
currentY = Mathf.Clamp(currentY, YMin, YMax);
Vector3 Direction = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
transform.position = lookAt.position + rotation * Direction;
transform.LookAt(lookAt.position);
}
}
Player Movement Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
CharacterController Controller;
public float Speed;
public Transform Cam;
// Start is called before the first frame update
void Start()
{
Controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
float Horizontal = Input.GetAxis("Horizontal") * Speed * Time.deltaTime;
float Vertical = Input.GetAxis("Vertical") * Speed * Time.deltaTime;
Vector3 Movement = Cam.transform.right * Horizontal + Cam.transform.forward * Vertical;
Movement.y = 0f;
Controller.Move(Movement);
if (Movement.magnitude != 0f)
{
transform.Rotate(Vector3.up * Input.GetAxis("Mouse X") * Cam.GetComponent<CameraMove>().sensivity * Time.deltaTime);
Quaternion CamRotation = Cam.rotation;
CamRotation.x = 0f;
CamRotation.z = 0f;
transform.rotation = Quaternion.Lerp(transform.rotation, CamRotation, 0.1f);
}
}
}
Hope i helped someone out there
if you want to share any improvements to my script feel free to do so
I put it together because I was curious. I didn’t comment on it because it is all super straightforward.
Enclosed please find the setup I used. I had to adjust mouse sensitivity WAY up to make it usable.
If you have questions about a line of code, work through the documentation for the functions you’re unsure about, or come ask a pointed question here.
@NairelPrandini Hope you don’t mind me repackaging your code and posting it back here.
can this be used for a mobile game with 1 left joystick for movement and to move camera left/right/up/down by touching the screen with your finger? I cant seem to find a good one ( or at least a working one…) that doesn’t involve using another characters scripts from unity store
It can but you might have better luck starting from something designed for touch screen, since the above is designed for mouse movement.
In my proximity_buttons project I have a small demo called DemoCameraRotatedControls.unity that sorta does this, but it is more of a top-down isometric controller, although you could probably hack it to do what you want.
proximity_buttons is presently hosted at these locations:
this tutorial worked for me ( you have to read a few comments cause they will help solve some issues ) but
everithing works perfectly ( except the camera does not move up and down ( but i fixed that once with raycast . ill see if it gets fixed once i write a raycast again , last time i did it for desktop but this time i’m doing it for mobile)
Step 3. Identify at least the ballpark area where the error is in your code. Extract the relevant section of code, as little as you think will show the problem, and post it here. If you don’t post code, we can’t help you. If you post massive amounts of code, we’ll assume you’re not even trying to be helpful. If nothing is happening, use Debug.Log() to find out if your code is even running.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Ok. I have very big question?
how to make a left and right controls for android and ios??
For example:
Left controls have a transparent for rotate a camera control , jump button etc…
Right controls have a joystick for walking and some buttons…
But I want:
First question: first show a joystick. And I press any ware to came joystick there, with on left side on display only
Second question : I will rotate a camera in right side only
And finally : I want aim in mid point on display
this might be a bit of a noob question but how do i implement this
like is there a way that i can tell it what the player is because when i add it to the main camera it doesnt give gibe any options help would be much apreciated
Certainly not by necro posting. Try this approach:
Imphenzia: How Did I Learn To Make Games:
Two steps to tutorials and / or example code:
do them perfectly, to the letter (zero typos, including punctuation and capitalization)
stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.