FollowPlayer Problem

Recommended Posts

I just made a script called FollowPlayer.cs. Here's the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FollowPlayer : MonoBehaviour {

public int speed;
public GameObject player;

void Update () {
Vector3 localPosition = player.transform.position - transform.position;
localPosition = localPosition.normalized; // The normalized direction in LOCAL space
transform.Translate(localPosition.x * Time.deltaTime * speed, localPosition.y * Time.deltaTime * speed, localPosition.z * Time.deltaTime * speed);
}
}



I added my gameObject and a capsule collider to the follow model, but whenever it follows me, it hovers in the air and tries to push me off the plane

Edited by Timothy Sharp

Share on other sites
3 hours ago, Timothy Sharp said:

I added my gameObject and a capsule collider to the follow model, but whenever it follows me, it hovers in the air and tries to push me off the plane

That is indeed what your script is telling it to do.

You will want a rigid body attached to the capsule to get it down to the ground.

For following use vector math instead of moving in the direction:

	public Transform Target; //This will be the player that you feed to the script
public float Lag = 50f;  //How much the object lags behind before catching up

Vector3 offset;
Rigidbody FollowerRigidbody; //Physics object needs to be moved by there physics body

// Use this for initialization
void Start () {
offset = this.transform.position - Target.position;
//Tis means how far between the two for example if x = 5 for player
//and x = 12 for follow object then 12 - 5 =7
//This means there is 7 spaces between Player and Follower
FollowerRigidbody = this.GetComponent<Rigidbody> (); // so we can call the physics object when needed//
}

void FixedUpdate(){
Vector3 TargetPosition = Target.position + offset; // So now if player moves we just get the player position and + our 7 to it
// this gives us the new destenation for the follower

FollowerRigidbody.MovePosition (Vector3.Lerp(this.transform.position,TargetPosition, Lag* Time.deltaTime));
//This is how we move. The lerp takes the old position and the new destenation and moves to it smoothly based on a lag factor.
//If it isn't a physics object we use:

//Lerp is a slider so if our first value is 0 and the second 10; then lerp(0,10, 0.5f) = 5
}

This can be used for camera also:

Spoiler


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour {

public Transform Target;
public float Lag = 50f;

Vector3 offset;

// Use this for initialization
void Start () {
offset = this.transform.position - Target.position;
}

void FixedUpdate(){
Vector3 TargetPosition = Target.position + offset; //Finds the relative target point
this.transform.position = Vector3.Lerp(this.transform.position,TargetPosition, Lag* Time.deltaTime);
}

// Update is called once per frame
void Update () {

}
}

If this is your first game this tutorial series will be great for you: https://unity3d.com/learn/tutorials/s/survival-shooter-tutorial

What I explained was this part: https://unity3d.com/learn/tutorials/projects/survival-shooter/camera-setup?playlist=17144 and they take there time to explain in detail.

Share on other sites
9 hours ago, Scouting Ninja said:

That is indeed what your script is telling it to do.

You will want a rigid body attached to the capsule to get it down to the ground.

For following use vector math instead of moving in the direction:


public Transform Target; //This will be the player that you feed to the script
public float Lag = 50f;  //How much the object lags behind before catching up

Vector3 offset;
Rigidbody FollowerRigidbody; //Physics object needs to be moved by there physics body

// Use this for initialization
void Start () {
offset = this.transform.position - Target.position;
//Tis means how far between the two for example if x = 5 for player
//and x = 12 for follow object then 12 - 5 =7
//This means there is 7 spaces between Player and Follower
FollowerRigidbody = this.GetComponent<Rigidbody> (); // so we can call the physics object when needed//
}

void FixedUpdate(){
Vector3 TargetPosition = Target.position + offset; // So now if player moves we just get the player position and + our 7 to it
// this gives us the new destenation for the follower

FollowerRigidbody.MovePosition (Vector3.Lerp(this.transform.position,TargetPosition, Lag* Time.deltaTime));
//This is how we move. The lerp takes the old position and the new destenation and moves to it smoothly based on a lag factor.
//If it isn't a physics object we use:

//Lerp is a slider so if our first value is 0 and the second 10; then lerp(0,10, 0.5f) = 5
}

This can be used for camera also:

Hide contents



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour {

public Transform Target;
public float Lag = 50f;

Vector3 offset;

// Use this for initialization
void Start () {
offset = this.transform.position - Target.position;
}

void FixedUpdate(){
Vector3 TargetPosition = Target.position + offset; //Finds the relative target point
this.transform.position = Vector3.Lerp(this.transform.position,TargetPosition, Lag* Time.deltaTime);
}

// Update is called once per frame
void Update () {

}
}

This doesn't work. I attached it to the follower. We both sink into the plane, and my actions affect his actions

If this is your first game this tutorial series will be great for you: https://unity3d.com/learn/tutorials/s/survival-shooter-tutorial

What I explained was this part: https://unity3d.com/learn/tutorials/projects/survival-shooter/camera-setup?playlist=17144 and they take there time to explain in detail.

Share on other sites

I don't know if you wanted to ask something.

To quote only a part of the forum, highlight it with your mouse and a "quote" popup will appear.

Welcome to the community and don't be afraid to ask or share ideas.

Share on other sites
On 2/12/2018 at 10:17 AM, Scouting Ninja said:

That is indeed what your script is telling it to do.

You will want a rigid body attached to the capsule to get it down to the ground.

For following use vector math instead of moving in the direction:


public Transform Target; //This will be the player that you feed to the script
public float Lag = 50f;  //How much the object lags behind before catching up

Vector3 offset;
Rigidbody FollowerRigidbody; //Physics object needs to be moved by there physics body

// Use this for initialization
void Start () {
offset = this.transform.position - Target.position;
//Tis means how far between the two for example if x = 5 for player
//and x = 12 for follow object then 12 - 5 =7
//This means there is 7 spaces between Player and Follower
FollowerRigidbody = this.GetComponent<Rigidbody> (); // so we can call the physics object when needed//
}

void FixedUpdate(){
Vector3 TargetPosition = Target.position + offset; // So now if player moves we just get the player position and + our 7 to it
// this gives us the new destenation for the follower

FollowerRigidbody.MovePosition (Vector3.Lerp(this.transform.position,TargetPosition, Lag* Time.deltaTime));
//This is how we move. The lerp takes the old position and the new destenation and moves to it smoothly based on a lag factor.
//If it isn't a physics object we use:

//Lerp is a slider so if our first value is 0 and the second 10; then lerp(0,10, 0.5f) = 5
}

This can be used for camera also:

Reveal hidden contents



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour {

public Transform Target;
public float Lag = 50f;

Vector3 offset;

// Use this for initialization
void Start () {
offset = this.transform.position - Target.position;
}

void FixedUpdate(){
Vector3 TargetPosition = Target.position + offset; //Finds the relative target point
this.transform.position = Vector3.Lerp(this.transform.position,TargetPosition, Lag* Time.deltaTime);
}

// Update is called once per frame
void Update () {

}
}

If this is your first game this tutorial series will be great for you: https://unity3d.com/learn/tutorials/s/survival-shooter-tutorial

What I explained was this part: https://unity3d.com/learn/tutorials/projects/survival-shooter/camera-setup?playlist=17144 and they take there time to explain in detail.

Thid doesn't work. It doesn't do the walking/running animation and it pushes you off the plane.

Share on other sites
1 hour ago, Timothy Sharp said:

Thid doesn't work. It doesn't do the walking/running animation and it pushes you off the plane.

The walking and running you will have to assign based on the velocity. Vectors are directions so it's really easy to do.

Can you show a screenshot of how you have setup things in Unity; the way you prepare things matter.

About the follower, is it going to be an AI?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 15
• 9
• 11
• 9
• 9
• Forum Statistics

• Total Topics
634134
• Total Posts
3015751
×