Question by
amseabrook · Feb 16, 2020 at 08:36 PM ·
newbiebuild and runfloatingmathf.sin
Object floats fine within Unity but constantly falls when running build version
Hey everyone,
I was wondering if you could help me, i'm very new at unity and i'm having a problem with an object that floats up and down. It seems to animate fine within the Unity preview but when I run a build version it constantly falls. My code is below —
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMotor : MonoBehaviour
{
private const float LANE_DISTANCE = 1.5f;
//Movement
private CharacterController controller;
private float speed = 7.0f;
private int desiredLane = 1; //0 = Left, 1 = Middle, 2 = Right
//Bob animation
private float verticalSpeed = 4f;
private float amplitude = 0.2f;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void Update()
{
//Gather the inputs on which lane we should be
if (Input.GetKeyDown(KeyCode.LeftArrow))
MoveLane(false);
if (Input.GetKeyDown(KeyCode.RightArrow))
MoveLane(true);
//Calculate where we should be in the future
Vector3 targetPosition = transform.position.z * Vector3.forward;
if (desiredLane == 0)
targetPosition += Vector3.left * LANE_DISTANCE;
else if (desiredLane == 2)
targetPosition += Vector3.right * LANE_DISTANCE;
// Let's calculate our move delta
Vector3 moveVector = Vector3.zero;
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
moveVector.y = Mathf.Sin(Time.realtimeSinceStartup * verticalSpeed) * amplitude;
moveVector.z = 0;
//Move the moon
controller.Move(moveVector * Time.deltaTime);
}
private void MoveLane(bool goingRight)
{
desiredLane += (goingRight) ? 1 : -1;
desiredLane = Mathf.Clamp(desiredLane, 0, 2);
}
}
Many thanks!
Andrew
note: just to follow up I think it's these two which seem to be conflicting
moveVector.x = (targetPosition - transform.position).normalized.x * speed;
moveVector.y = Mathf.Sin(Time.realtimeSinceStartup * verticalSpeed) * amplitude;
Comment