private void Update() { //Input var targetDirectionOffset = Quaternion.identity; Vector2 camVector = GameInput.CameraVector(CtrlType); Vector3 orientedCamVector = new Vector3(camVector.x, 0, camVector.y); if (orientedCamVector != Vector3.zero) { Quaternion camQuaternion = Quaternion.Slerp(Quaternion.identity, Quaternion.LookRotation(orientedCamVector), orientedCamVector.magnitude); targetDirectionOffset = camQuaternion; } if (Target != null) { //Rotate the camera towards the velocity of the rigidbody //Set the up vector, and make it lerp towards the target's up vector if the target has a Ball Vector3 targetUp = Vector3.up; Ball bc = Target.GetComponent <Ball>(); if (bc) { targetUp = bc.Up; } up = Vector3.Lerp(up, targetUp, Time.deltaTime * 100); //Based on how fast the target is moving, create a rotation bending towards its velocity. Quaternion towardsVelocity = (Target.velocity != Vector3.zero) ? Quaternion.LookRotation(Target.velocity, up) : Quaternion.identity; const float maxTrans = 20f; Quaternion finalTargetDir = Quaternion.Slerp(currentDirection, towardsVelocity, Mathf.Max(0, Mathf.Min(-10 + Target.velocity.magnitude, maxTrans) / maxTrans)); //Lerp towards the final rotation currentDirection = Quaternion.Slerp(currentDirection, finalTargetDir, Time.deltaTime * 4); //Look for a BallControlInput and set its look direction BallControlInput bci = Target.GetComponent <BallControlInput>(); if (bci != null) { bci.LookDirection = currentDirection; } //Set camera FOV to get higher with more velocity AttachedCamera.fieldOfView = Mathf.Lerp(AttachedCamera.fieldOfView, Mathf.Min(60f + (Target.velocity.magnitude), 100f) + fovOffset, Time.deltaTime * 20); currentDirectionWithOffset = Quaternion.Slerp(currentDirectionWithOffset, currentDirection * targetDirectionOffset, Time.deltaTime * 6); transform.position = Target.transform.position + Vector3.up * orbitHeight + currentDirectionWithOffset * (Vector3.back * orbitDistance); transform.rotation = currentDirectionWithOffset; } }
private void Start() { Up = Vector3.up; //Set up drifty smoke smoke = Instantiate(prefabs.Smoke); smoke.target = this; smoke.DriftAudio = sounds.Brake; //Grab reference to Rigidbody rb = GetComponent <Rigidbody>(); //Set angular velocity (This is necessary for fast) rb.maxAngularVelocity = 1000f; //Set object name gameObject.name = type.ToString() + " - " + nickname; //Set character if (CharacterId >= 0 && CharacterId < ActiveData.Characters.Length) { SetCharacter(ActiveData.Characters[CharacterId]); } //Set up speed effect speedFire = Instantiate(prefabs.SpeedFire); speedFire.Init(this); //Crimbus DateTime now = DateTime.Now; if (now.Month == 12 && now.Day > 20 && now.Day <= 31) { hatPrefab = ActiveData.ChristmasHat; } if (ActiveData.GameSettings.eSportsReady) { hatPrefab = ActiveData.ESportsHat; } //Spawn hat if (hatPrefab) { GameObject hat = Instantiate(hatPrefab); hat.transform.SetParent(transform, false); } //Create objects and components based on ball type if (type == BallType.Player) { if (ctrlType != ControlType.None) { IBallCamera camera; //Create camera if (ActiveData.GameSettings.useOldControls) { camera = Instantiate(prefabs.OldCamera); ((PivotCamera)camera).UseMouse = ctrlType == ControlType.Keyboard; } else { camera = Instantiate(prefabs.Camera); } camera.Target = rb; camera.CtrlType = ctrlType; if (CameraCreated != null) { CameraCreated(this, new CameraCreationArgs(camera)); } } } if (type == BallType.LobbyPlayer) { //Make the lobby camera follow this ball var cam = FindObjectOfType <LobbyCamera>(); if (cam) { cam.AddBall(this); } } if ((type == BallType.Player || type == BallType.LobbyPlayer) && ctrlType != ControlType.None) { //Create input component input = gameObject.AddComponent <BallControlInput>(); } if (type == BallType.AI) { //Create AI component gameObject.AddComponent <BallControlAI>(); } }