Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post was deleted by Reddit.

2
Newbie trying to animate object on mouse click. Using OnMouseClick()
Post Flair (click to view more posts with a particular flair)
Post Body

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;

}

}

Author
Account Strength
60%
Account Age
2 years
Verified Email
Yes
Verified Flair
No
Total Karma
72
Link Karma
129
Comment Karma
n/a
Profile updated: 5 days ago
Posts updated: 11 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago