Unity stop object at position after move - c#

I just started learning Unity and C#
I'm trying to move my sprite over a line I draw on-screen with the mouse, it looks like the script is more or less working, but I would like to keep the GameObject at the position it is at the end of the line when it finishes the movement.
As you can see from the picture every time it finishes the animation it goes back to its original position (0,0).
How can I stop this behavior?
using System.Collections;
using System.Collections.Generic;
using Unity.Burst.Intrinsics;
using Unity.VisualScripting;
using UnityEngine;
public class Follow : MonoBehaviour
{
private LineRenderer lineRenderer;
public GameObject objectToMove;
public float speed = 5f;
private Vector3[] positions = new Vector3[397];
private Vector3[] pos;
private int index = 0;
private GameObject toFollow;
DrawLines drawLines;
[SerializeField] GameObject gameController;
private bool isDrawingLineDone = false;
// Start is called before the first frame update
void Awake()
{
drawLines = gameController.GetComponent<DrawLines>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) == true) // caso linea si e click
{
clearLines();
}
if (drawLines.isLinePresent == true && Input.GetMouseButtonUp(0) == true)
{
if (!isDrawingLineDone)
{
pos = GetLinePointsInWorldSpace();
objectToMove.transform.position = pos[index];
isDrawingLineDone = true;
}
}
if (isDrawingLineDone == true && finishMove == false)
{
Move();
}
else if (isDrawingLineDone == true && finishMove == true)
{
objectToMove.transform.position = pos[pos.Length - 1];
}
}
Vector3[] GetLinePointsInWorldSpace()
{
//Get the positions which are shown in the inspector
toFollow = GameObject.Find("Linea");
lineRenderer = toFollow.GetComponent<LineRenderer>();
lineRenderer.GetPositions(positions);
//the points returned are in world space
return positions;
}
// MOve the gameObject
private bool finishMove = false;
void Move()
{
objectToMove.transform.position = Vector3.MoveTowards(objectToMove.transform.position,
pos[index],
speed * Time.deltaTime);
if (objectToMove.transform.position == pos[index])
{
index += 1;
if (objectToMove.transform.position == pos[pos.Length - 1])
{
print("DONE");
finishMove = true;
}
}
if (index == pos.Length)
{
index = 0;
}
}
// delete other line if there is
private GameObject toDestroy;
void clearLines()
{
toDestroy = GameObject.Find("Linea");
Destroy(toDestroy);
drawLines.isLinePresent = false;
}
}

Related

Stopping player's movement upon clicking dialogue - Unity

I have a problem where when I click the dialogue while passing through an NPC or while moving, the character will continue moving in the direction of the joystick before it is set active to false.
I have tried setting the horizontal and vertical input to zero and even the movement direction which is the calculated horizontal and vertical input, but the character still moves in the last direction it was moving. I'm not sure if the if statement that I created is correct but that was the idea that came to mind.
This is the script for the movement of the player:
using UnityEngine;
using UnityEngine.EventSystems;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private GameObject interactButton;
public float speed, rotationSpeed, ySpeed, originalStepOffset;
public Joystick joystick;
private Animator animator;
private CharacterController characterController;
void Start()
{
animator = GetComponent<Animator>();
characterController = GetComponent<CharacterController>();
originalStepOffset = characterController.stepOffset;
}
// Update is called once per frame
void FixedUpdate()
{
float horizontalInput = joystick.Horizontal;
float verticalInput = joystick.Vertical;
Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput);
movementDirection = Quaternion.Euler(0, 45f, 0) * movementDirection;
float magnitude = Mathf.Clamp01(movementDirection.magnitude) * speed;
movementDirection.Normalize();
ySpeed += Physics.gravity.y * Time.deltaTime;
if(characterController.isGrounded)
{
characterController.stepOffset = originalStepOffset;
ySpeed = -0.5f;
}
else
{
characterController.stepOffset = 0;
}
Vector3 velocity = movementDirection * magnitude;
velocity = AdjustVelocityToSlope(velocity);
velocity.y += ySpeed;
//transform.Translate(movementDirection * speed * Time.deltaTime, Space.World);
characterController.Move(velocity * Time.deltaTime);
if (movementDirection != Vector3.zero)
{
if(EventSystem.current.currentSelectedGameObject != null)
{
if (EventSystem.current.currentSelectedGameObject.name == "InteractButton")
{
horizontalInput = 0f;
verticalInput = 0f;
movementDirection = Vector3.zero;
animator.SetBool("IsMoving", false);
}
else
{
//transform.forward = movementDirection;
animator.SetBool("IsMoving", true);
Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
}else
{
animator.SetBool("IsMoving", true);
Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
}
} else
{
animator.SetBool("IsMoving", false);
}
}
private Vector3 AdjustVelocityToSlope(Vector3 velocity)
{
var ray = new Ray(transform.position, Vector3.down);
if(Physics.Raycast(ray, out RaycastHit hitInfo, 0.2f))
{
var slopeRotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);
var adjustedVelocity = slopeRotation * velocity;
if(adjustedVelocity.y < 0)
{
return adjustedVelocity;
}
}
return velocity;
}
}
The function that will make the button to set active to true and add its onclick listeners is a trigger enter function and a trigger exit for setting it active to false and removing the onclick listeners. It is attached to multiple NPC whenever I go near them, the button will appear. In case someone needs to see the script for the button here it is, but I'm gonna paste only until the related part:
using System;
using System.Collections;
using System.Drawing;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[System.Serializable]
public class DialogueManager : MonoBehaviour
{
/*
// NPC INTERACTION
private static DialogueManager buttonOwner;
private Camera mainCamera;
*/
// NPC DATA
private Character characterJson;
// FOR DIALOG FLOW
private int multiDialogCycle;
public static int dialogId;
// GAME OBJECTS REFERENCE
[SerializeField] private GameObject dialogBox, addPanel, m1, id, darkPanel;
[SerializeField] private Button nameBtn, choiceOneBtn, choiceTwoBtn;
[SerializeField] private TextMeshProUGUI npcName, choiceOne, choiceTwo, dialogName, dialogMessage, addedText;
[SerializeField] private TextAsset characterData;
// FOR MULTIPLE DIALOGUES WITHOUT CHOICES
private bool clickEnable;
private string[] multiDialog;
// CHOICES STORAGE
private static Choice[] choices = new Choice[2];
//private TryInstantiate tryInstantiate = new TryInstantiate();
private GameData gameData;
private void Start()
{
// LOAD NPC DATA
characterJson = JsonUtility.FromJson<Character>(characterData.text);
// SET DEFAULT VALUE
//dialogId = 0;
multiDialogCycle = 0;
clickEnable = false;
// SET SCRIPTABLE OBJECT FOR DATA STORAGE
gameData = Resources.Load<GameData>("GameData/GameData");
}
private void Update()
{
if(nameBtn.gameObject.activeInHierarchy)
{
if(Input.GetKey(KeyCode.Space))
{
EventSystem.current.SetSelectedGameObject(nameBtn.gameObject);
}
}
// FOR ENDING DIALOGUES WITHOUT CHOICES
if (Input.GetMouseButtonDown(0) && clickEnable == true)
{
if (multiDialog.Length > 1 )
{
if(getDialog(dialogId).choices.Length == 0 && multiDialogCycle == multiDialog.Length - 1)
{
addArticles();
addObjectives();
addClues();
closeDialog();
Debug.Log(getDialog(dialogId).minigame);
}
else
{
if(getDialog(dialogId).minigame != "" && multiDialogCycle < multiDialog.Length - 1)
{
if(!gameData.idShown)
{
darkPanel.SetActive(true);
id.SetActive(true);
}
}else
{
multiDialogCycle++;
loadCharacterData();
}
}
}
else
{
if (getDialog(dialogId).minigame != "")
{
if(getDialog(dialogId).minigame == "spot_object")
{
m1.SetActive(true);
}
dialogBox.SetActive(false);
gameData.dialogActive = false;
}
else
{
addArticles();
addObjectives();
addClues();
closeDialog();
}
}
}
if(gameData.idShown)
{
multiDialogCycle++;
loadCharacterData();
gameData.idShown = false;
}
if (gameData.dialogActive && dialogId != 0 && getDialog(dialogId).minigame != "" && !gameData.loadedData)
{
updateID();
loadCharacterData();
}
// FOR NPC NAMES
/*
if (buttonOwner != this) return;
if (!mainCamera) mainCamera = Camera.main;
var position = mainCamera.WorldToScreenPoint(head.position + offset);
//uiUse.transform.position = position;
nameBtn.transform.position = position;
*/
}
private void OnTriggerEnter(Collider collisionInfo)
{
if (collisionInfo.CompareTag("Player"))
{
nameBtn.gameObject.SetActive(true);
nameBtn.GetComponent<Image>().sprite = Resources.Load<Sprite>("InteractionAsset/DIALOGUE");
nameBtn.transform.GetChild(0).GetComponent<TextMeshProUGUI>().color = new Color32(75,75,75,255);
//buttonOwner = this;
nameBtn.onClick.RemoveListener(onNameClick);
nameBtn.onClick.AddListener(onNameClick);
choiceOneBtn.onClick.RemoveListener(onChoiceClick);
choiceOneBtn.onClick.AddListener(onChoiceClick);
choiceTwoBtn.onClick.RemoveListener(onChoiceClick);
choiceTwoBtn.onClick.AddListener(onChoiceClick);
npcName.text = characterJson.name;
}
}
private void OnTriggerExit(Collider collisionInfo)
{
if (collisionInfo.CompareTag("Player"))
{
nameBtn.onClick.RemoveListener(onNameClick);
choiceOneBtn.onClick.RemoveListener(onChoiceClick);
choiceTwoBtn.onClick.RemoveListener(onChoiceClick);
nameBtn.gameObject.SetActive(false);
//buttonOwner = null;
}
}
// DIALOGUE SYSTEM
public void onNameClick()
{
nameBtn.gameObject.SetActive(false);
dialogBox.SetActive(true);
gameData.dialogActive = true;
FindObjectOfType<AudioManager>().Play("ButtonSound");
if (dialogBox.activeInHierarchy)
{
if (dialogMessage != null && dialogName != null)
{
loadCharacterData();
interactedNPC();
}
else
{
// Debug.Log("null dialog message");
}
}
}
public void updateID()
{
if (gameData.win && !gameData.loadedData)
{
dialogId = gameData.targetId_1;
gameData.loadedData = true;
}
else if (!gameData.win && !gameData.loadedData)
{
dialogId = gameData.targetId_2;
gameData.loadedData = true;
}
}
How can I atleast reset the Joystick's position. I am currently using the Joystick Pack from Unity Asset Store.
I for one would start the Conversation flow from the dialogue step. Once the dialog starts, you can either set a bool or any other type of property (even a referenced object such as the dialogue itself or the NPC that it's chatting to, but a bool is simpler) that would be a marker. Simply put: public bool IsTalking;. This could be in PlayerMovement or the main Player-like component (or SO) which you can access from PlayerMovement via GameObject or a different public variable.
Next, once the dialogue mechanism starts, you can set the marker (such as the bool or NPC ref) in the Player-like component or PlayerMovement to true, false when the chatting stops.
Then in PlayerMovement.FixedUpdate() you can just stop code execution:
// Update is called once per frame
void FixedUpdate()
{
// or if (Player.IsTalking == true) if you want the marker there,
// but you can have it inside PlayerMovement if you wish as long as
// you can reference it without overhead - meaning don't do crazy
// stuff like searching all game objects/components for PlayerMovement)
if (IsTalking == true)
{
if (characterController != null)
characterController.Move(Vector3.zero);
if (animator != null)
animator.SetBool("IsMoving", false);
return;
}
// everything starts executing once the player stops talking to NPCs
float horizontalInput = joystick.Horizontal;
float verticalInput = joystick.Vertical;
Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput);
movementDirection = Quaternion.Euler(0, 45f, 0) * movementDirection;
float magnitude = Mathf.Clamp01(movementDirection.
As I understood you dialogue window is modal. Depending on your needs you can stop the internal increment of time progression with
Time.timeScale = 0f;
But this will stop e.g. (UI) animations and the proper function of the event system, so some people use
Time.timeScale = 0.0001f;
This has the drawback that the gameplay continues slowly and our player hero could be hit by a very slow rocket while the user was resting in a pause screen.

AI wandering state doesn't seem to work right

I've been trying to make an AI system that would follow specific waypoints and once the player character walks into the trigger zone, the AI would start chasing the character and vice versa.
It seems that this does kind of work, but the AI only moves to one waypoint and then stops until the player walks into the trigger zone; after which if the player walks out, it again only goes to one waypoint then stops again.
I have multiple waypoints and I just want it to keep cycling through them, but it just goes to one of them and stops.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
using UnityEngineInternal;
public class AILocomotion : MonoBehaviour
{
public Transform playerTransform;
NavMeshAgent agent;
Animator animator;
public float maxTime = 1.0f;
public float maxDistance = 1.0f;
float timer = 0.0f;
bool moveTowards = false;
bool followWaypoint = false;
float deaccel= 0.5f;
float calVelocity = 0.0f;
public Transform[] points;
private int destPoint = 0;
public string animState;
void Start()
{
agent = GetComponent<NavMeshAgent>();
animator = GetComponent<Animator>();
moveTowards = false;
followWaypoint = true;
}
// Update is called once per frame
void Update()
{
if (moveTowards == true)
{
timer -= Time.deltaTime;
if (timer < 0.0f)
{
float sqDistance = (playerTransform.position - agent.destination).sqrMagnitude;
if (sqDistance > maxDistance * maxDistance)
{
agent.destination = playerTransform.position;
}
}
animator.SetFloat("Speed", agent.velocity.magnitude);
}
else if (!moveTowards && agent.velocity.magnitude>0.0f)
{
calVelocity = agent.velocity.magnitude;
calVelocity -= Time.deltaTime * deaccel;
animator.SetFloat("Speed", calVelocity);
}
//CHECKS IF BOTH CONDITIONS HAVE MET AND RUNS THE WAYPOINT FOLLOWING CODE
if (followWaypoint == true && (!agent.pathPending && agent.remainingDistance < 1.0f))
{
GotoNextPoint();
}
Debug.LogError("Bool" + followWaypoint);
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
moveTowards = true;
//DISABLES THE WAYPOINT FOLLOWING CODE TO RUN THE CHASE CODE INSTEAD
followWaypoint = false;
}
}
private void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
moveTowards = false;
//RE-ENABLES THE WAYPOINT FOLLOWING CODE ONCE THE PLAYER LEAVES THE TRIGGER AREA
followWaypoint = true;
}
}
//THIS IS THE WAYPOINT FOLLOWING CODE
void GotoNextPoint()
{
animator.SetFloat("Speed", agent.velocity.magnitude);
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
Debug.LogError("DestPoint = " + destPoint);
// Choose the next point in the array as the destination.
// cycling to the start if necessary.
destPoint = Random.Range(0, points.Length);
}
}
Use OnTriggerStay instead of OntriggerEnter and Try

How to utilize stacking blocks under the player object(cylinder) by using rigidbodies in unity3d?

I am working on a hypercasual game project which is very similar to Matching Cubes. Initially, I was using transform to stack blocks under my cylinder(player object). But in the game there will be a ramp to jump through it and by using transform I was ignoring the physics and it passes through the ramp. So I changed it a little bit by utilizing the rigidbody for the cylinder. If there is no block it jumps through the ramp but I need it to jump with blocks. The problem is I couldn't find a way to stack them under the cylinder by using rigidbody. Tried MovePosition or AddForce but it does not work at all.
How can I stack the blocks under the cylinder but also make them jump through the ramp together?
Here is my StackManager.cs . There is a Regulator function that will check the 'picks' list and regulate the positions. It is the function where I handle all positioning.
I also tried making the positioning by transform and when it collides with ramp, stop the Regulator() and AddForce(Vector3.up*offset) but it did not move up a bit.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
public class StackManager : MonoBehaviour
{
public static StackManager instance;
[SerializeField] private float distanceBetweenObjs;
[SerializeField] private Transform prevObject = null;
[SerializeField] private Transform parent;
[SerializeField] private Transform cylinder;
[SerializeField] private Transform trailer;
private List<Transform> picks; // Use this to order gate and random gate
private Vector3 firstPosition;
private int comboCounter; // fever mode tracker
private Rigidbody rb;
private Transform prev;
private void Awake() {
if(instance == null) {
instance = this;
}
}
void Start()
{
rb = cylinder.gameObject.GetComponent<PlayerController>().rb;
comboCounter = 0;
picks = new List<Transform>();
firstPosition = new Vector3(cylinder.position.x, cylinder.position.y, cylinder.position.z);
}
// Update is called once per frame
void Update()
{
Regulator();
}
void CheckChildren() {
List<Transform> children = new List<Transform>();
foreach (Transform child in picks)
{
children.Add(child);
}
for(int i = 0; i < children.Count - 2; i++) {
if (children[i].isSameMaterial2(children[i+1], children[i+2])) {
comboCounter++;
Destroy(children[i].gameObject);
Destroy(children[i+1].gameObject);
Destroy(children[i+2].gameObject);
picks.Remove(children[i]);
picks.Remove(children[i+1]);
picks.Remove(children[i+2]);
};
}
if(comboCounter == 3) {
SpeedBoost(); //fever mode
comboCounter = 0;
}
}
public void PickUp(GameObject pickedObj){
pickedObj.tag = "Picked";
pickedObj.transform.parent = parent;
picks.Add(pickedObj.transform);
}
private void Regulator(){
//Position of cylinder
//set y value based on the # of children objects
/**
*hold the first position of cylinder
*make calculation by referencing it
*reference + localScale.y + 0.1f:
*/
Vector3 newPos = new Vector3(cylinder.position.x, firstPosition.y, cylinder.position.z);
foreach (Transform child in picks)
{
newPos.y += child.localScale.y + 0.1f;
}
//cylinder.position = newPos;
rb.MovePosition(newPos);
//Position of children
if(picks.Count>0) {
prevObject = picks[picks.Count-1];
}
/**
*For each child
* cylinder-0.1f-pick-0.1f-pick-...
*/
prev = cylinder;
for(int i = 0; i < picks.Count; i++)
{
if(i==0){
picks[i].position = new Vector3(prev.position.x, prev.position.y-1.2f, prev.position.z);
//picks[i].gameObject.GetComponent<Rigidbody>().position(new Vector3(prev.position.x, prev.position.y-1.2f, prev.position.z));
} else {
//picks[i].gameObject.GetComponent<Rigidbody>().MovePosition(new Vector3(prev.position.x, prev.position.y-prev.localScale.y -0.1f, prev.position.z));
picks[i].position = new Vector3(prev.position.x, prev.position.y-prev.localScale.y -0.1f, prev.position.z);
}
prev = picks[i];
}
//Position of trailer object
if(picks.Count>0) {
trailer.position = new Vector3(prev.position.x, prev.position.y-0.2f, prev.position.z); //relocate the trailer object under last pick
trailer.GetComponent<TrailRenderer>().material = prev.GetComponent<MeshRenderer>().material; //change the color of the trail
}
CheckChildren(); //check for the 3-conjugate combo
}
public void ChangeRampState() {
Debug.Log("prev.gameObject");
Debug.Log(prev.gameObject);
prev.gameObject.GetComponent<Collider>().isTrigger = false;
rb.AddForce(Vector3.up * 300);
}
public void OrderPicks() {
picks.Sort((x, y) => string.Compare(x.GetComponent<MeshRenderer>().sharedMaterial.name, y.GetComponent<MeshRenderer>().sharedMaterial.name));
}
public void ShufflePicks() {
picks = picks.Fisher_Yates_CardDeck_Shuffle();
}
async public void onObstacle(Transform pickToDestroy) {
pickToDestroy.gameObject.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
pickToDestroy.parent = null;
await Task.Delay(200);
picks.Remove(pickToDestroy);
}
public void SpeedBoost() {
cylinder.gameObject.GetComponent<PlayerController>().rb.velocity *= 2;
}
}

How can I move the Main Camera to a new position?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayingInGameScenesController : MonoBehaviour
{
public LockController lockController;
public Vector3 targetPosition;
public GameObject uiTextsImage;
public float cameraMoveSpeed;
private bool newGame = true;
private bool playingScene = true;
public void PlayingSceneInGame()
{
PlayingSceneStatesControls(true);
StartCoroutine(ScenePlayingTime());
}
private void Update()
{
if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true)
{
PlayingSceneInGame();
newGame = false;
}
if(playingScene == false)
{
// X 40.73769 to -6 Y 1.1 to 1 Z -4.641718 to 43.4
Vector3 camPos = Camera.main.transform.position;
Vector3 newPos = new Vector3(camPos.x)
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, targetPosition, cameraMoveSpeed * Time.deltaTime);
}
}
private void PlayingSceneStatesControls(bool LockState)
{
lockController.LockControl(LockState);
if (LockState == true)
{
// change state of free look camera height and view distance.
// change the cameras states enable true/false.
// change the ui texts for description text and scene text enable true/false.
uiTextsImage.SetActive(true);
}
else
{
uiTextsImage.SetActive(false);
playingScene = false;
}
}
IEnumerator ScenePlayingTime()
{
yield return new WaitForSeconds(10);
PlayingSceneStatesControls(false);
}
}
In the Update :
if(playingScene == false)
{
// X 40.73769 to -6 Y 1.1 to 1 Z -4.641718 to 43.4
Vector3 camPos = Camera.main.transform.position;
Vector3 newPos = new Vector3(camPos.x)
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, targetPosition, cameraMoveSpeed * Time.deltaTime);
}
First should I move the two variables camPos and newPos to the Start ?
Second the values X Y Z the left values is where the camera is position at now and the right values where to should move to smooth slowly. From the current position on X the current position is 40.73769 to -6 same for the Y and Z I just wrote this values to remember but the idea is to move smooth slowly the camera from the current position to the new position : X -6 Y 1 and Z to 43.4
Should I do it in the Update or Late or Fixed Update ?
This is the main camera original position :
And this is the target position I want the main camera to get I entered the new target position values :
What I tried :
In the Start :
private void Start()
{
Vector3 camPos = Camera.main.transform.position;
newPos = new Vector3(camPos.x - 46.73769f, camPos.y - 0.1f, camPos.z + 48.041718f);
}
Then in the Update :
if(playingScene == false)
{
// X 40.73769 - 46.73769 Y 1.1 - 0.1 Z -4.641718 + 48.041718
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, newPos, cameraMoveSpeed * Time.deltaTime);
}
Then I changed the values in the Start switched Z and X :
private void Start()
{
Vector3 camPos = Camera.main.transform.position;
newPos = new Vector3(camPos.x - 48.041718f, camPos.y - 0.1f, camPos.z + 46.73769f);
}
In both cases the main camera start from some other position not the original position at all and ending in other position :
Starting somewhere under/inside the spaceship :
And ending here :
Working :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayingInGameScenesController : MonoBehaviour
{
public GameObject camera;
public LockController lockController;
public GameObject uiTextsImage;
public float transitionSpeed = 5f;
public Transform currentView;
// The initial offset from the target.
private Vector3 offset;
private bool newGame = true;
private bool playingScene = true;
private Vector3 newPos;
private void Start()
{
currentView.position = new Vector3(43.4f, 1f,-6f);
offset = camera.transform.position - currentView.position;
}
public void PlayingSceneInGame()
{
PlayingSceneStatesControls(true);
StartCoroutine(ScenePlayingTime());
}
private void Update()
{
if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true)
{
PlayingSceneInGame();
newGame = false;
}
}
private void LateUpdate()
{
if (playingScene == false)
{
//Lerp position
camera.transform.position = Vector3.Lerp(camera.transform.position, currentView.position, Time.deltaTime * transitionSpeed);
}
}
private void PlayingSceneStatesControls(bool LockState)
{
lockController.LockControl(LockState);
if (LockState == true)
{
// change state of free look camera height and view distance.
// change the cameras states enable true/false.
// change the ui texts for description text and scene text enable true/false.
uiTextsImage.SetActive(true);
}
else
{
uiTextsImage.SetActive(false);
playingScene = false;
}
}
IEnumerator ScenePlayingTime()
{
yield return new WaitForSeconds(10);
PlayingSceneStatesControls(false);
}
}

Unity - how do i move the pencil higher then the mouse

The problem is that the pencil it's way too close to the mousePosition. i want to put the pencil higher than the mousePosition.
here is an image as reference : https://ibb.co/s6v25t9
The thing i want to achieve, it's the pencil to be higher than the line drawn
This is taken from a project. it's not my own code but i am trying to figure out how to fix some bugs and this one i cant really figure out how to fix
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.IO;
public class GameManager : MonoBehaviour
{
[Tooltip("The color of the drawn lines")]
public Color lineColor;
public Material lineMaterial;
public Transform Pencil;
public Sprite SurpriseGlass;
public Sprite HappyGlass;
public Sprite SadGlass;
public Slider PenCapacity;
public Text PenPercent;
[HideInInspector]
public GameObject[] Hint;
public Image Star1;
public Image Star2;
public Image Star3;
public GameObject LevComp;
private GameObject[] Obs;
private List<GameObject> listLine = new List<GameObject>();
public List<Vector2> listPoint = new List<Vector2>();
private GameObject currentLine;
public GameObject currentColliderObject;
private GameObject hintTemp;
private GameObject[] waterTap;
private GameObject Glass;
private Vector3 LastMosPos;
private BoxCollider2D currentBoxCollider2D;
private LineRenderer lines;
private LineRenderer currentLineRenderer;
private bool stopHolding;
private bool allowDrawing = true;
[HideInInspector]
public bool completed;
int clickCont;
private List<Rigidbody2D> listObstacleNonKinematic = new List<Rigidbody2D>();
private GameObject[] obstacles;
float mosDis;
bool canCreate;
RaycastHit2D hit_1;
RaycastHit2D hit_2;
RaycastHit2D hit_3;
GameObject TemLine;
void Start()
{
Pencil.gameObject.SetActive(false);
waterTap = GameObject.FindGameObjectsWithTag("Interactive");
Glass = GameObject.FindGameObjectWithTag("GlassParent");
Glass.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
Hint = GameObject.FindGameObjectsWithTag("Hint");
for (int i = 0; i < Hint.Length; i++)
{
Hint[i].SetActive(false);
}
lineMaterial.SetColor("_Color", lineColor);
Obs = GameObject.FindGameObjectsWithTag("Obstacle");
for (int i = 0; i < Obs.Length; i++)
{
Obs[i].GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
}
}
void Update()
{
if (PenCapacity.value <= 0.01f || !Input.GetMouseButton(0))
{
Pencil.gameObject.SetActive(false);
}
if (Input.GetMouseButtonDown(0))
{
GameObject thisButton = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject; //Get the button on click
if (thisButton != null) //Is click on button
{
allowDrawing = false;
print("cant drwa");
}
else //Not click on button
{
allowDrawing = true;
stopHolding = false;
listPoint.Clear();
CreateLine(Input.mousePosition);
print("draw");
}
}
else if (Input.GetMouseButton(0) && !stopHolding && allowDrawing && PenCapacity.value > 0)
{
RaycastHit2D rayHit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (LastMosPos != Camera.main.ScreenToWorldPoint(Input.mousePosition))
{
if (rayHit.collider == null)
{
Pencil.gameObject.SetActive(true);
Pencil.position = new Vector3(LastMosPos.x, LastMosPos.y, 0);
if (canCreate == false)
{
float dist = Vector3.Distance(LastMosPos, Camera.main.ScreenToWorldPoint(Input.mousePosition));
Pencil.GetComponent<TrignometricRotation>().enabled = true;
PenCapacity.value = PenCapacity.value - dist / 25;
PenPercent.text = Mathf.FloorToInt(PenCapacity.value * 100).ToString() + " %";
if (Mathf.FloorToInt(PenCapacity.value * 100) < 75)
{
Star3.gameObject.SetActive(false);
}
if (Mathf.FloorToInt(PenCapacity.value * 100) < 50)
{
Star2.gameObject.SetActive(false);
}
if (Mathf.FloorToInt(PenCapacity.value * 100) < 25)
{
Star1.gameObject.SetActive(false);
}
}
}
}
else
{
Pencil.GetComponent<TrignometricRotation>().enabled = false;
}
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float ab = Vector2.Distance(LastMosPos, mousePos);
mosDis = mosDis + ab;
if (!listPoint.Contains(mousePos) && mosDis > .02f)
{
mosDis = 0;
//Add mouse pos, set vertex and position for line renderer
if (canCreate == false)
{
if (rayHit.collider == null)
{
listPoint.Add(mousePos);
}
In the code you posted the value of LastMosPos is never changed so I can't say where you get it from.
However instead of the line
Pencil.position = new Vector3(LastMosPos.x, LastMosPos.y, 0);
you probably could simply add a certain offset like e.g.
Pencil.position = new Vector3(LastMosPos.x, LastMosPos.y + 20f, 0);
to make the pencil appear 20 Unity units (in a Screenspace Overlay canvas this euqals 20 Pixels) above the LastMosPos.

Categories