I’m a beginner, so I try to make the scripts as simple as possible.
Well, I try to make a simple smooth follow camera. However I don’t know how to rotate the camera with the character while keeping the camera behind the character.
The camera and the character should face the same way.
This is my script…
using UnityEngine;
using System.Collections;
public class Follow : MonoBehaviour {
[SerializeField]
private Transform Target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.position = new Vector3 (Target.position.x , Target.position.y + 2, Target.position.z - 10);
}
}
For the moment, the camera keep a good distance with the object and follow it quite well, but the issue is the rotations. When I rotate my character the camera doesn’t stay behind it.
You’re making it so that the camera holds the same positional relationship to the character in WorldSpace, which is essentially saying “ten feet south of object, two feet up”. What you need is to set the relationship as being the same using LocalSpace instead, which would be “ten feet behind object, two feet up”. The following should do what you want:
using UnityEngine;
using System.Collections;
public class Follow : MonoBehaviour {
[SerializeField]
private Transform Target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Target){
transform.position = Target.TransformPoint(new Vector3( 0f, 2f, -10f));
transform.LookAt(Target);
}
}
}
Basically each object has its own “local space” that’s orientated in the direction it’s facing. If I’m standing straight and facing north, then my local space is going to be identical to “world space” (the normal universal coordinate system), except that the origin (0,0,0) is my current location. If I turn west, then my local space is now facing in that direction, so Z+ (forward) is now pointing west, as apposed to the World Space which is still Z+ (forward) is “north”.
Local space is important if the direction that the object is facing is going to influence the values you need (in other words, if you want to use things like “forward”, “backward”, “left”, and “right” from the object’s point of view). If that doesn’t matter, in other words you want to use words like “west”, “north”, etc… (universal relative positions), then you don’t need to bother with local space and can instead just use the transform’s “position” attribute, which is its absolute position using Unity’s global coordinate system (world space coordinates).
The camera works well with my character movement, but when I click on my mouse to rotate the camera independently of my character, that doesn’t work, the camera doesn’t rotate anymore.
My rotation code.
using UnityEngine;
using System.Collections;
public class Rotation : MonoBehaviour {
public Quaternion Origin;
// Use this for initialization
void Start () {
Origin = transform.rotation;
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0))
{
transform.Rotate (new Vector3 (Input.GetAxis ("Mouse Y") * 250, Input.GetAxis ("Mouse X") * 250, 0) * Time.deltaTime);
}
else
{
transform.rotation = Quaternion.Slerp (transform.rotation, Origin, Time.deltaTime * 5);
}
}
}
I tried few things, I think I know where’s the trouble, but I can’t find any solution to fix it.
This code try to move the camera, but the other stick it behind the character.
I think you’re running into a logic problem here, rather than a programming problem. I had assumed you just wanted the camera to be directly behind the character at a specific height and distance all the time (like an over-the-shoulder view). But no, you apparently want the camera to be rotatable on its own, but you ALSO want it to follow behind the character and rotate when the character rotates. That’s slightly more difficult- you’re getting into behaviours built on top of other behaviours now.
You need to figure out in plain text exactly how you want the camera to act- do you want it to be independently rotatable but then slowly “drift” back to the normal “behind the character” position? Do you want manual changes to stick, no drifting, until the character starts moving at which point it starts drifting? Do you want it to never drift, but you want both changes to the character’s rotation and manual changes to both move the camera around? First figure out, in pure logic and text, every facet of the camera’s actions and control that you want. After you decide all of that, then you can start thinking about programming code.
Basically, I want that the camera is always behind the character like a third person game, but like many MMO’s, I would like to rotate the camera when I press on the mouse and move it. A bit like World of warcraft. I found some scripts , but I’ll not learn if I use this kind of codes. I try to make my own. However I’m stuck, I can’t figure out how to do that.
The easy way is making it so that the other script is completely disabled while the button is being held down- just use
GetComponent<Follow>().enabled = false;
after the if (Input.GetMouseButton (0)) bit and turn it back on when/if it’s not held down anymore.
You could also make a sort of “camera state” variable- a bool or an enum or something, and then have both scripts check the value of that to decide which one has control. Alternatively, you could just combine the scripts and use a simple if/then statement in the update. shrugs
There’s literally a dozen ways or more to do what you want, so you can just play around with it until you figure out how you want to handle it.
I already tried that, but the camera doesn’t follow the character at all. The camera could be at 100 feet behind the character. Honestly, I tried few things, but I can’t find any solution.
What you’re wanting is a camera that has several behaviours depending on whether the target is moving or stationary and whether input is forcing it to turn or not. That logic is likely too complicated to realistically expect yourself to be able to come up with on your own when you’re just starting out, I agree. However, it’s also unrealistic to expect the community to piece together a script for you from scratch just because you decided that don’t want to use one of the myriad of existing controller scripts out there (there’s even one in the Standard Assets included in Unity).
At this point we’re just building the exact same kind of thing one piece at a time for you, and if “doing it on your own” is the goal, then this is counterproductive. I highly suggest that you look at the scripts that you can find around the net and study how they work one line at a time, checking the manual for functions that you don’t recognize, and learn that way.