This post was deleted by Reddit.
I have a 3D object, broken into 5 parts. When a user clicks on the object, it opens and shows internals. This works fine using OnMouseDown(). What I'm trying to do is make these objects slowly move when clicked, as opposed to just jumping to the finish point. Below is the code. What am I doing wrong here? I haven't really worked on cleaning up the code as I'm trying to just get the functionality to work.
I've uploaded the code to GitHub as well so you can see all the scripts that are in the project.
GothardJ2/BatteryBreakdown: Code for the battery breakdown (github.com)
using System.Collections;using System.Collections.Generic;using UnityEngine;
public class OpenBattery : MonoBehaviour
{
[SerializeField] float moveSpeed = .05f;
[SerializeField] GameObject top;
[SerializeField] GameObject board;
[SerializeField] GameObject cells;
[SerializeField] GameObject cells;[SerializeField] GameObject bottom;
public bool isOpen = false;
public ExtendCells extendCells;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnMouseDown()
{
if (isOpen == false)
{
OpenBatteryCase();
}
else if (isOpen && extendCells.isExtended == false)
{
CloseBatteryCase();
}
}
private void CloseBatteryCase()
{
Vector3 topXMovement = new Vector3(-0.99f, 0f, 0f);
Vector3 boardXMovement = new Vector3(-0.68f, 0f, 0f);
Vector3 bottomXMovement = new Vector3(0.79f, 0f, 0f);
top.transform.Translate(topXMovement);
board.transform.Translate(boardXMovement);
bottom.transform.Translate(bottomXMovement);
isOpen = false;
}
private void OpenBatteryCase()
{
Debug.Log("Close");
Vector3 topXMovement = new Vector3(0.99f, 0f, 0f);
Vector3 boardXMovement = new Vector3(0.68f, 0f, 0f);
Vector3 bottomXMovement = new Vector3(-0.79f, 0f, 0f);
top.transform.Translate(topXMovement * moveSpeed * Time.deltaTime);
board.transform.Translate(boardXMovement * moveSpeed * Time.deltaTime);
bottom.transform.Translate(bottomXMovement * moveSpeed * Time.deltaTime);
isOpen = true;
}
}
Post Details
- Posted
- 2 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/unity/comme...