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

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);
}
}

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.

Unity stop object at position after move

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;
}
}

When enabling the OrbitCam script it's changing the camera view and position how can I enable the it but keep the camera view and position?

The game start when the camera is at this position :
Then the camera move smooth slowly to this position :
Using this script :
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)
{
uiTextsImage.SetActive(true);
}
else
{
uiTextsImage.SetActive(false);
playingScene = false;
}
}
IEnumerator ScenePlayingTime()
{
yield return new WaitForSeconds(10);
PlayingSceneStatesControls(false);
}
}
Now at this point I want to check and know when the camera is not moving anymore in the LateUpdate and then when the camera stopped moving to enable true the OrbitCam script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrbitCam : MonoBehaviour
{
//All the variables used in this class(Look below to see what they do. :D )
private const float Y_ANGLE_MIN = 0.0f;
private const float Y_ANGLE_MAX = 50.0f;
private const float DISTANCE_MAX = 10.0f;
private const float DISTANCE_MIN = 0.1f;
private const float TRANS_MIN = 1.0f;
private const float TRANS_MAX = 2.0f;
public Transform lookAt;
public Transform camTransform;
public GameObject player;
private Camera cam;
public float distance = 5.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
private float sensitivityX = 4.0f;
private float sensitivityY = 1.0f;
private float trandis;
public Vector3 height = new Vector3(0, 0, 0);
private bool below = false;
private List<Renderer> playerRenderers = new List<Renderer>();
private void Start()
{
//Makes camTransform a transform. :)
camTransform = transform;
//Sets variable cam value to the main camera
cam = Camera.main;
foreach (Transform child in player.transform)
{
if (child.GetComponent<Renderer>() != null)
{
playerRenderers.Add(child.GetComponent<Renderer>());
}
}
}
private void Update()
{
//Makes the camera move by looking at the axis of the mouse(Also multiplied by the seisitivity.)
currentX += Input.GetAxis("Mouse X") * sensitivityX;
currentY += Input.GetAxis("Mouse Y") * sensitivityY;
//Limits the Y variable
currentY = Mathf.Clamp(currentY, Y_ANGLE_MIN, Y_ANGLE_MAX);
//Thiago Laranja's scrollwheel implemetation.
if (Input.GetAxis("Mouse ScrollWheel") > 0) { distance += 0.2f; }
if (Input.GetAxis("Mouse ScrollWheel") < 0) { distance -= 0.2f; }
//Makes sure that these variables never go over the max and be les than the min. :)
distance = Mathf.Clamp(distance, DISTANCE_MIN, DISTANCE_MAX);
trandis = Mathf.Clamp(distance, TRANS_MIN, TRANS_MAX) - 1;
for (int i = 0; i < playerRenderers.Count; i++)
{
//Sets players transparency(Make sure that player materials rendering mode has set to transparent or other mode that supports transparency).
playerRenderers[i].material.color = new Color(playerRenderers[i].material.color.r, playerRenderers[i].material.color.g, playerRenderers[i].material.color.b, trandis);
//Disables the object from rendering if your're at distance 0.8.
if (distance <= 0.8f) { playerRenderers[i].enabled = false; }
if (distance > 0.8f) { playerRenderers[i].enabled = true; }
}
//If close enough to the character sinp into distance of 0.1(If distance is 0 the camera cant be rotated.)
if (distance <= 0.8f && below == false) { distance = 0.1f; below = true; }
if (distance >= 0.8f && below == true) { below = false; }
}
private void LateUpdate()
{
//Subtracts hte distance from Z coordinate
Vector3 dir = new Vector3(0, 0, -distance);
//Creates an quaternion for rotation(too bad that we cannot use Vector3. :D )
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
//Sets the cameras position and makes it look at player.
camTransform.position = lookAt.position + height + rotation * dir;
camTransform.LookAt(lookAt.position + height);
}
}
I don'y know how to check when the camera finished/stopped moving to the target position ?
When I'm enabling the OrbitCam for now I'm doing it manual checking the enable box in the editor the OrbitCam change the camera view and position and I want that when I'm enabling the OrbitCam script that it will keep the view and position of the camera in the second screenshot. Not to keep that view and position all the time only for starting the gameplay part. but what I'm getting when enabling the OrbitCam script is :
I'd strongly recommend to use a animation on the camera. This has the big advantage that you can change the camera position like you do in the script and you have the option to set animation events. So you can call a function at the end of the animation. So if the camera animation is over, the camera is now in the correct position, you can enable the orbit cam. It is also very simple to set up! Hope that helps you!

Why when the camera is child of the character only when pressing on W to move forward the character is not moving and everything is stuttering?

This is the character settings and components :
The character don't move and everything stuttering only when the Camera is child of the character.
If the camera is not child of the character the character will move fine but I want the camera to follow the character.
Maybe I should edit and add the ThirdPersonCharacter script ? It's attached to the character. The script is a bit long.
There are 3 scripts they are all connected a bit long but they are all making the character controlling.
ThirdPersonUserControl :
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
namespace UnityStandardAssets.Characters.ThirdPerson
{
[RequireComponent(typeof (ThirdPersonCharacter))]
public class ThirdPersonUserControl : MonoBehaviour
{
private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
private Transform m_Cam; // A reference to the main camera in the scenes transform
private Vector3 m_CamForward; // The current forward direction of the camera
private Vector3 m_Move;
private bool m_Jump; // the world-relative desired move direction, calculated from the camForward and user input.
private void Start()
{
// get the transform of the main camera
if (Camera.main != null)
{
m_Cam = Camera.main.transform;
}
else
{
Debug.LogWarning(
"Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
// we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
}
// get the third person character ( this should never be null due to require component )
m_Character = GetComponent<ThirdPersonCharacter>();
}
private void Update()
{
if (!m_Jump)
{
m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
}
}
// Fixed update is called in sync with physics
private void FixedUpdate()
{
// read inputs
float h = CrossPlatformInputManager.GetAxis("Horizontal");
float v = CrossPlatformInputManager.GetAxis("Vertical");
bool crouch = Input.GetKey(KeyCode.C);
// calculate move direction to pass to character
if (m_Cam != null)
{
// calculate camera relative direction to move:
m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
m_Move = v*m_CamForward + h*m_Cam.right;
}
else
{
// we use world-relative directions in the case of no main camera
m_Move = v*Vector3.forward + h*Vector3.right;
}
#if !MOBILE_INPUT
// walk speed multiplier
if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f;
#endif
// pass all parameters to the character control script
m_Character.Move(m_Move, crouch, m_Jump);
m_Jump = false;
}
}
}
AICharacterControl :
using System;
using UnityEngine;
namespace UnityStandardAssets.Characters.ThirdPerson
{
[RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))]
[RequireComponent(typeof (ThirdPersonCharacter))]
public class AICharacterControl : MonoBehaviour
{
public UnityEngine.AI.NavMeshAgent agent { get; private set; } // the navmesh agent required for the path finding
public ThirdPersonCharacter character { get; private set; } // the character we are controlling
public Transform target; // target to aim for
private void Start()
{
// get the components on the object we need ( should not be null due to require component so no need to check )
agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>();
character = GetComponent<ThirdPersonCharacter>();
agent.updateRotation = false;
agent.updatePosition = true;
}
private void Update()
{
if (target != null)
agent.SetDestination(target.position);
if (agent.remainingDistance > agent.stoppingDistance)
character.Move(agent.desiredVelocity, false, false);
else
character.Move(Vector3.zero, false, false);
}
public void SetTarget(Transform target)
{
this.target = target;
}
}
}
ThirdPersonCharacter :
using UnityEngine;
namespace UnityStandardAssets.Characters.ThirdPerson
{
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
[RequireComponent(typeof(Animator))]
public class ThirdPersonCharacter : MonoBehaviour
{
[SerializeField] float m_MovingTurnSpeed = 360;
[SerializeField] float m_StationaryTurnSpeed = 180;
[SerializeField] float m_JumpPower = 12f;
[Range(1f, 4f)][SerializeField] float m_GravityMultiplier = 2f;
[SerializeField] float m_RunCycleLegOffset = 0.2f; //specific to the character in sample assets, will need to be modified to work with others
[SerializeField] float m_MoveSpeedMultiplier = 1f;
[SerializeField] float m_AnimSpeedMultiplier = 1f;
[SerializeField] float m_GroundCheckDistance = 0.1f;
Rigidbody m_Rigidbody;
Animator m_Animator;
bool m_IsGrounded;
float m_OrigGroundCheckDistance;
const float k_Half = 0.5f;
float m_TurnAmount;
float m_ForwardAmount;
Vector3 m_GroundNormal;
float m_CapsuleHeight;
Vector3 m_CapsuleCenter;
CapsuleCollider m_Capsule;
bool m_Crouching;
void Start()
{
m_Animator = GetComponent<Animator>();
m_Rigidbody = GetComponent<Rigidbody>();
m_Capsule = GetComponent<CapsuleCollider>();
m_CapsuleHeight = m_Capsule.height;
m_CapsuleCenter = m_Capsule.center;
m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
m_OrigGroundCheckDistance = m_GroundCheckDistance;
}
public void Move(Vector3 move, bool crouch, bool jump)
{
// convert the world relative moveInput vector into a local-relative
// turn amount and forward amount required to head in the desired
// direction.
if (move.magnitude > 1f) move.Normalize();
move = transform.InverseTransformDirection(move);
CheckGroundStatus();
move = Vector3.ProjectOnPlane(move, m_GroundNormal);
m_TurnAmount = Mathf.Atan2(move.x, move.z);
m_ForwardAmount = move.z;
ApplyExtraTurnRotation();
// control and velocity handling is different when grounded and airborne:
if (m_IsGrounded)
{
HandleGroundedMovement(crouch, jump);
}
else
{
HandleAirborneMovement();
}
ScaleCapsuleForCrouching(crouch);
PreventStandingInLowHeadroom();
// send input and other state parameters to the animator
UpdateAnimator(move);
}
void ScaleCapsuleForCrouching(bool crouch)
{
if (m_IsGrounded && crouch)
{
if (m_Crouching) return;
m_Capsule.height = m_Capsule.height / 2f;
m_Capsule.center = m_Capsule.center / 2f;
m_Crouching = true;
}
else
{
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
{
m_Crouching = true;
return;
}
m_Capsule.height = m_CapsuleHeight;
m_Capsule.center = m_CapsuleCenter;
m_Crouching = false;
}
}
void PreventStandingInLowHeadroom()
{
// prevent standing up in crouch-only zones
if (!m_Crouching)
{
Ray crouchRay = new Ray(m_Rigidbody.position + Vector3.up * m_Capsule.radius * k_Half, Vector3.up);
float crouchRayLength = m_CapsuleHeight - m_Capsule.radius * k_Half;
if (Physics.SphereCast(crouchRay, m_Capsule.radius * k_Half, crouchRayLength, Physics.AllLayers, QueryTriggerInteraction.Ignore))
{
m_Crouching = true;
}
}
}
void UpdateAnimator(Vector3 move)
{
// update the animator parameters
m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
m_Animator.SetBool("Crouch", m_Crouching);
m_Animator.SetBool("OnGround", m_IsGrounded);
if (!m_IsGrounded)
{
m_Animator.SetFloat("Jump", m_Rigidbody.velocity.y);
}
// calculate which leg is behind, so as to leave that leg trailing in the jump animation
// (This code is reliant on the specific run cycle offset in our animations,
// and assumes one leg passes the other at the normalized clip times of 0.0 and 0.5)
float runCycle =
Mathf.Repeat(
m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime + m_RunCycleLegOffset, 1);
float jumpLeg = (runCycle < k_Half ? 1 : -1) * m_ForwardAmount;
if (m_IsGrounded)
{
m_Animator.SetFloat("JumpLeg", jumpLeg);
}
// the anim speed multiplier allows the overall speed of walking/running to be tweaked in the inspector,
// which affects the movement speed because of the root motion.
if (m_IsGrounded && move.magnitude > 0)
{
m_Animator.speed = m_AnimSpeedMultiplier;
}
else
{
// don't use that while airborne
m_Animator.speed = 1;
}
}
void HandleAirborneMovement()
{
// apply extra gravity from multiplier:
Vector3 extraGravityForce = (Physics.gravity * m_GravityMultiplier) - Physics.gravity;
m_Rigidbody.AddForce(extraGravityForce);
m_GroundCheckDistance = m_Rigidbody.velocity.y < 0 ? m_OrigGroundCheckDistance : 0.01f;
}
void HandleGroundedMovement(bool crouch, bool jump)
{
// check whether conditions are right to allow a jump:
if (jump && !crouch && m_Animator.GetCurrentAnimatorStateInfo(0).IsName("Grounded"))
{
// jump!
m_Rigidbody.velocity = new Vector3(m_Rigidbody.velocity.x, m_JumpPower, m_Rigidbody.velocity.z);
m_IsGrounded = false;
m_Animator.applyRootMotion = false;
m_GroundCheckDistance = 0.1f;
}
}
void ApplyExtraTurnRotation()
{
// help the character turn faster (this is in addition to root rotation in the animation)
float turnSpeed = Mathf.Lerp(m_StationaryTurnSpeed, m_MovingTurnSpeed, m_ForwardAmount);
transform.Rotate(0, m_TurnAmount * turnSpeed * Time.deltaTime, 0);
}
public void OnAnimatorMove()
{
// we implement this function to override the default root motion.
// this allows us to modify the positional speed before it's applied.
if (m_IsGrounded && Time.deltaTime > 0)
{
Vector3 v = (m_Animator.deltaPosition * m_MoveSpeedMultiplier) / Time.deltaTime;
// we preserve the existing y part of the current velocity.
v.y = m_Rigidbody.velocity.y;
m_Rigidbody.velocity = v;
}
}
void CheckGroundStatus()
{
RaycastHit hitInfo;
#if UNITY_EDITOR
// helper to visualise the ground check ray in the scene view
Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance));
#endif
// 0.1f is a small offset to start the ray from inside the character
// it is also good to note that the transform position in the sample assets is at the base of the character
if (Physics.Raycast(transform.position + (Vector3.up * 0.1f), Vector3.down, out hitInfo, m_GroundCheckDistance))
{
m_GroundNormal = hitInfo.normal;
m_IsGrounded = true;
m_Animator.applyRootMotion = true;
}
else
{
m_IsGrounded = false;
m_GroundNormal = Vector3.up;
m_Animator.applyRootMotion = false;
}
}
}
}
In the script ThirdPersonCharacter there is no Update function at all.
In the AICharacterControl there is Update I tried to change it to FixedUpdate but it didn't fix the problem.
In the script ThirdPersonUserControl there is Update but it's only for Jump and there is also FixedUpdate so I didn't try to change anything in that script.
Try using FixedUpdate() instead of Update() and see. Also, you should at least post the relevant bit of code.

LineRender | collision himself with raycast2D

i build game with line that you can control his movement.
every few seconeds the line create hole for the player can move through them.
look picture:
enter image description here
now in my code you can see the playerMovement, createLine and drawLine.
i draw the line By player location and every few seconeds i stop draw to millie seconed to create hole and than draw new line.
my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DrawLineNisayon : MonoBehaviour
{
[SerializeField] private GameObject linePrefab;
LineRenderer lineRenderer;
public GameObject currentLine;
private bool createHole = false;
private bool drawLine = false;
float rotZ = 0f;
public float speedMovement = 3f;
public float speedRotation = 6f;
public float timingCreateHoles = 2f;
public float widthHoles = 0.2f;
public EdgeCollider2D edgeCollider2D;
public List<Vector2> posLine;
void Start()
{
CreateLine();
}
void Update()
{
PlayerMovement(speedMovement);
DrawLine();
}
private void CreateLine()
{
currentLine = Instantiate(linePrefab, Vector3.zero, Quaternion.identity);
lineRenderer = currentLine.GetComponent<LineRenderer>();
edgeCollider2D = currentLine.GetComponent<EdgeCollider2D>();
posLine.Clear();
posLine.Add(transform.position);
posLine.Add(transform.position);
lineRenderer.SetPosition(0, posLine[0]);
lineRenderer.SetPosition(1, posLine[1]);
edgeCollider2D.points = posLine.ToArray();
}
private void DrawLine()
{
if(!drawLine)
{
posLine.Add(transform.position);
lineRenderer.positionCount++;
lineRenderer.SetPosition(lineRenderer.positionCount - 1, transform.position);
edgeCollider2D.points = posLine.ToArray();
RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.right, 1f);
Debug.DrawRay(posLine[lineRenderer.positionCount - 1], transform.right);
if(createHole == false){
StartCoroutine(DrawHole(timingCreateHoles));
createHole = true;
}
}
}
private IEnumerator DrawHole(float timingCreateHoles)
{
yield return new WaitForSeconds(timingCreateHoles);
drawLine = true;
StartCoroutine(DrawLine(widthHoles));
}
private IEnumerator DrawLine(float widthHoles)
{
yield return new WaitForSeconds(widthHoles);
CreateLine();
drawLine = false;
createHole = false;
}
private void PlayerMovement(float speed)
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
if(Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.A))
{
transform.Rotate(0f, 0f, rotZ);
rotZ = speedRotation;
}
if (!Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A))
{
transform.Rotate(0f, 0f, rotZ);
rotZ = -speedRotation;
}
}
}
so what is my problem? i try to create raycast to check collision with the line himself like the game "snake". i tryed to create raycast in function "DrawLine" and i havnt idea how to do that.
my line built by list of vector2 like you can see in my code, its say that i need to compare vector to vector and i dont know how...
*i try collision with the last index in line, not with the circle in the picture.

Categories