Advertisement

FollowPlayer Problem

Started by February 12, 2018 12:27 PM
5 comments, last by Scouting Ninja 6 years, 9 months ago

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

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.

Advertisement
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.

 

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.:)

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.

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?

This topic is closed to new replies.

Advertisement