Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
public class CameraMovement : MonoBehaviour {
float inputHorizontal;
float inputVertical;
float sensitivityX = 4;
float sensitivityY = 2;
float horizontalRotation;
float verticalRotation;
float yMin = 0;
float yMax = 80;
float distanceFromTarget = 10;
float rotationSmoothTime = 0.12f;
Vector3 currentRotation;
Vector3 rotationSmoothVelocity;
GameObject playerObject;
private void Start()
{
playerObject = GameObject.FindGameObjectWithTag(StringCollection.PLAYER);
}
private void LateUpdate()
{
if (playerObject != null)
{
horizontalRotation += Input.GetAxisRaw(StringCollection.MOUSE_X) * sensitivityX;
verticalRotation -= Input.GetAxisRaw(StringCollection.MOUSE_Y) * sensitivityY;
verticalRotation = Mathf.Clamp(verticalRotation, yMin, yMax);
currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(verticalRotation, horizontalRotation), ref rotationSmoothVelocity, rotationSmoothTime);
transform.eulerAngles = currentRotation;
transform.position = playerObject.transform.position - transform.forward * distanceFromTarget;
}
}
}
public class PlayerMovement : Creature
{
private float movementSpeed = 8;
private float currentMovementSpeed;
private float speedSmoothTime = 0.1f;
private float turnSmoothTime = 0.1f;
private float inputHorizontal;
private float inputVertical;
private float jumpPower = 10;
private float airControlPercentage = 0.2f;
private bool isGrounded;
private bool jumpPressed;
private float turnSmoothVelocity;
private float speedSmoothVelocity;
Rigidbody playerRigid;
Transform cameraTransform;
private void Start()
{
playerRigid = gameObject.GetComponent<Rigidbody>();
cameraTransform = Camera.main.gameObject.transform;
}
private void Update()
{
if (Input.GetButtonDown(StringCollection.JUMP))
jumpPressed = true;
}
private void FixedUpdate()
{
isGrounded = GroundCheck();
Vector2 inputDirection = (new Vector2(Input.GetAxisRaw(StringCollection.HORIZONTAL), Input.GetAxisRaw(StringCollection.VERTICAL))).normalized;
if (isGrounded && jumpPressed)
playerRigid.velocity = new Vector3(playerRigid.velocity.x, jumpPower, playerRigid.velocity.z);
jumpPressed = false;
if (inputDirection != Vector2.zero)
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, Mathf.Atan2(inputDirection.x, inputDirection.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
currentMovementSpeed = Mathf.SmoothDamp(currentMovementSpeed, movementSpeed * inputDirection.magnitude, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
playerRigid.velocity = transform.forward * currentMovementSpeed + Vector3.up * playerRigid.velocity.y;
currentMovementSpeed = (new Vector2(playerRigid.velocity.x, playerRigid.velocity.z)).magnitude;
}
private float GetModifiedSmoothTime(float smoothTime)
{
if (isGrounded)
return smoothTime;
if (airControlPercentage == 0)
return float.MaxValue;
return smoothTime / airControlPercentage;
}
private bool GroundCheck()
{
return true;
}
}
C#-Quelltext |
|
1 |
transform.lookAt(playerObject); |
Denn sobald "Mario" links um die Ecke läuft guckt die Kamera ja gegen die Ecke. Quasi.
Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von »Renegade« (19.07.2017, 14:12)
Werbeanzeige