Jump to content
Unity Insider Forum

KameraScript Problem...


Phash

Recommended Posts

Servus

 

ich mache gerade ein Tutorial und habe dort eine einfache MainKamera mit einem einfachen Script erstellt:

using UnityEngine;
using System.Collections;
public class CamController : MonoBehaviour {
public Transform Target;
// Use this for initialization
void Start () {

}
public float Distance = 10.0f;
// Update is called once per frame
void LateUpdate () {
 if (!Target)
 {
  return;
 }
 transform.position = Target.position - Distance * transform.forward;
}
}

ich weise dem Script meine Spielfigur zu, und die Kamer folgt dem Spieler. Allerdings rotiert sie nicht mit. Klar.

 

Nach ein paar Recherchen kam ich auf das SmoothFollowScript

using UnityEngine;
using System.Collections;
public class SmoothFollowCSharp : MonoBehaviour
{
/*
 This camera smoothes out rotation around the y-axis and height.
 Horizontal Distance to the target is always fixed.

 There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.

 For every of those smoothed values we calculate the wanted value and the current value.
 Then we smooth it using the Lerp function.
 Then we apply the smoothed values to the transform's position.
 */

// The target we are following
public Transform target;
// The distance in the x-z plane to the target
public float distance = 10.0f;
// the height we want the camera to be above the target
public float height = 5.0f;
// How much we
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;

void  LateUpdate ()
{
 // Early out if we don't have a target
 if (!target)
  return;

 // Calculate the current rotation angles
 float wantedRotationAngle = target.eulerAngles.y;
 float wantedHeight = target.position.y + height;
 float currentRotationAngle = transform.eulerAngles.y;
 float currentHeight = transform.position.y;

 // Damp the rotation around the y-axis
 currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

 // Damp the height
 currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

 // Convert the angle into a rotation
 Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

 // Set the position of the camera on the x-z plane to:
 // distance meters behind the target
 transform.position = target.position;
 transform.position -= currentRotation * Vector3.forward * distance;

 // Set the height of the camera
 transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);

 // Always look at the target
 transform.LookAt (target);
}
}

Das Script ist in Unity als js vorhanden, ich habe die C# Version genutzt.

 

Was jetzt passiert: meine Spielfigur dreht sich zwar noch, wenn ich die Pfeiltasten nutze, allerdings bewegt sich die Figur nicht vom Fleck.

 

Mit der einfachen Kamera geht es, mit dem smoothFollow Script nicht.

 

Der Spieler ist der Target Variable zugewiesen.

Hat jemand eine Idee, was hier los ist?

 

using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {

private Drive drive;
private Weapon weapon;
// Use this for initialization
void Start () {
	drive = GetComponent<Drive>();
	weapon = GetComponent<Weapon>();
}

// Update is called once per frame
void Update () {
	if (weapon)
	{
		if (Input.GetKeyDown(KeyCode.Space))
		{
			weapon.StartFiring();
		}
		if (Input.GetKeyUp(KeyCode.Space))
		{
			weapon.StopFiring();
		}
	}
	if (drive)
	{
		if (Input.GetKey(KeyCode.UpArrow))
		{
			drive.Accelerate(1.0f);
		}
		if (Input.GetKey(KeyCode.DownArrow))
		{
			drive.Deaccelerate(1.0f);
		}
		if (Input.GetKey(KeyCode.RightArrow))
		{
			drive.RotateRight(1.0f);
		}
		if (Input.GetKey(KeyCode.LeftArrow))
		{
			drive.RotateLeft(1.0f);
		}
	}
}
}

 

using UnityEngine;
using System.Collections;
public class Drive : MonoBehaviour {
public float Acceleration = 1000.0f;
public float Deacceleration = 1000.0f;
public float RotationAcceleration = 300.0f;
public float MaxForce = 1000.0f;
public float MinForce = -500.0f;
public float CurrentForce = 0.0f;
private bool hasChangedCurrentForce = false;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update ()
{
	rigidbody.AddForce(CurrentForce * Time.deltaTime * transform.forward);
	if (!hasChangedCurrentForce)
	{
		var forwardFactor = Vector3.Dot(transform.forward, rigidbody.velocity);
		if (forwardFactor > 1.0f)
		{
			Deaccelerate(1.0f);
		}
		else if (forwardFactor < -1.0f)
		{
			Accelerate(1.0f);
		}
		else
		{
			CurrentForce = 0;
		}
	}
	hasChangedCurrentForce = false;
}
public void Accelerate(float factor)
{
	CurrentForce += Time.deltaTime * Acceleration * factor;
	CurrentForce = Mathf.Clamp(CurrentForce, MinForce, MaxForce);
	hasChangedCurrentForce = true;
}
public void Deaccelerate(float factor)
{
	CurrentForce -= Time.deltaTime * Deacceleration * factor;
	CurrentForce = Mathf.Clamp(CurrentForce, MinForce, MaxForce);
	hasChangedCurrentForce = true;
}
public void RotateRight(float factor)
{
	rigidbody.AddTorque(0, Time.deltaTime * RotationAcceleration * factor, 0);
}
public void RotateLeft(float factor)
{
	rigidbody.AddTorque(0, -Time.deltaTime * RotationAcceleration * factor, 0);
}
}

 

btw.: die Force wird auf den Spieler angewandt. Er bewegt sich nur einfach nicht...

Link zu diesem Kommentar
Auf anderen Seiten teilen

update.... dieses Script hier stört:

 

using UnityEngine;
using System.Collections;
public class ProjectToPlane : MonoBehaviour {
   public GameObject ObjectToProject;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    var plane = new Plane(Vector3.up, 0);
    float distance;
    if (plane.Raycast(new Ray(transform.position, transform.forward), out distance))
    {
	    ObjectToProject.transform.position = transform.position + transform.forward * distance;
    }
}
}

 

wie könnte man dieses Script mit dem smooth Follow zusammenbringen?

Link zu diesem Kommentar
Auf anderen Seiten teilen

Archiviert

Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.

×
×
  • Neu erstellen...