Re-coded camera problems

Hi guys,

I made a game where the player is represented by a ball that rolls around and is directed by the camera (the ball rolls towards what the camera is looking), so I decided I needed to re-code the camera so it can:
-follow the player
-turn around the player with left and right arrow
So that is what I did BUT I’m having problems with the camera because it does strange thing when the player is moving. And when I say move I mean on every axis because I coded a jump for the player too…

Does somebody have an idea of how I could re-code the camera to make it less weird?

PS:The code is in the attached file

2941078–217666–CameraFollow.cs (942 Bytes)

What you are describing is basically a third person camera. Take a look at the Unity Standard Assets. There is a Third Person Camera there that you can use as an example.

First off you should check out this page for posting code in the forums. People are more likely to help if they don’t have to download a separate file:

I created a Scene with a Generic Cube. I stick a small cylinder in the front of it.
I used this basic code to move the cube around:
PlayerMove.cs

using UnityEngine;
using System.Collections;

public class PlayerMove : MonoBehaviour {

    public float speed;
   
    void Update () {
        Vector3 movement = Vector3.zero;
        if (Input.GetKey(KeyCode.W))
            movement = transform.forward;
        if (Input.GetKey(KeyCode.S))
            movement = -transform.forward;
        if (Input.GetKey(KeyCode.A))
            movement = -transform.right;
        if (Input.GetKey(KeyCode.D))
            movement = transform.right;
        if (Input.GetKey(KeyCode.Q))
            movement = transform.up;
        if (Input.GetKey(KeyCode.E))
            movement = -transform.up;

        transform.position += movement * speed * Time.deltaTime;

    }
}

I then copied your code from above and attached it to my camera. Everything worked perfectly your code seems fine. So there is no problem with your camera code. Maybe your movement script is off? Maybe some script is interfering, its hard to say. But I can say the posted camera code works just fine.

Thanks Dennis59, I’ll go check that out :wink:

takatok the problem is that my player is a ball that rolls around, so when I move the camera while I’m rolling forward it goes away…
So it will work with that basic movement code, but it won’t with my rolling around code.

Maybe post your movement code

I think I don’t have the required skill to understand the code I found…

Here’s the movement code

using UnityEngine;
using System.Collections;

public class PlayerMouv : MonoBehaviour
{
    public Rigidbody Forces;
    GameObject MainCamera;
    public float speed;
    public Vector3 Dirrection, velocity, spawnPoint;
    public bool toucheLeSol, dansSpawn, KillOn;

    void Start()
    {
        Forces=GetComponent<Rigidbody>();
        MainCamera=GameObject.Find("Main Camera");
        toucheLeSol=false;
        dansSpawn=false;
        KillOn=true;
        speed=2;
        spawnPoint=transform.position;
    }
    void FixedUpdate()
    {
        Dirrection=transform.position-MainCamera.transform.position;
        if(Input.GetKey(KeyCode.UpArrow))
        {
            Forces.AddForce(Dirrection.x*speed,0,Dirrection.z*speed);
        }
        if(Input.GetKey(KeyCode.DownArrow))
        {
            Forces.AddForce(-Dirrection.x*speed,0,-Dirrection.z*speed);
        }
    }
    void OnCollisionStay(Collision col)
    {
        if(col.contacts[0].normal.y<=1.5 && col.contacts[0].normal.y>=0.5)
        {
            toucheLeSol=true;
        }
    }
}

It’s full of variables that I use for other scripts so don’t mind that
Oh and I’m french so…yeah…“toucheLeSol” x)