GameObject follow canvas image position - c#

I have a gameObject that I would like to move between X=-2 and X=+2. It follows the position of a canvas Image that also moves in X axis as X=12 and X=150. How do I make sure my gameObject follows the right axis of the image without parenting it? It should be clamped between -2 & +2. So how do I do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowImagePosition : MonoBehaviour
{
public GameObject followImage;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void LateUpdate()
{
this.transform.position = new Vector3(followImage.transform.position.x, this.transform.position.z, this.transform.position.z);
}
}

Related

Random collectable items falling in the Camera View but not in the Game View

I am using a tutorial to make random GoodItems spawn from spawn points.
I see them falling at the spawnpoints in the Scene but not in the game view, they are just invisible. What's going on?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawn : MonoBehaviour
{
public Transform[] spawnPoints;
public GameObject goodItems;
// Start is called before the first frame update
void Start()
{
int randomIndex = Random.Range(0, spawnPoints.Length);
for (int i = 0; i < spawnPoints.Length; i++)
{
if (randomIndex != i)
{
Instantiate(goodItems, spawnPoints[i].position, Quaternion.identity);
}
}
}
// Update is called once per frame
void Update()
{
}
}
Hard to say, but perhaps the z-axis values of the spawnPoints objects are behind camera's z-axis value or camera's near clipping plane, or far away from camera's far clipping plane

my camera no longer displays my background

so i wrote a sript that when my Player touches my enemy. my Player respawn should know that my camera is following my player. But when I respawn, she moves behind my background
how to do?
My scipt
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Respawn : MonoBehaviour
{
[SerializeField] Transform spawnPoint;
void OnCollisionEnter2D(Collision2D col)
{
if (col.transform.CompareTag("Player"))
col.transform.position = spawnPoint.position;
}
}
//Thanks
There are two things you could do:
Use z axis to place object (player) in front.
Use sorting layers and sorting group component.

Make the CC Collider follow the camera (UNITY)

So I made a script that should In theory make the Character Controller's Collider follow the Player camera. Here is the Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class CCCameraFollower : MonoBehaviour
{
public GameObject Camera;
public CharacterController character;
// Start is called before the first frame update
void Start()
{
character = GetComponent<CharacterController>();
}
// Update is called once per frame
void LateUpdate()
{
character.center = Camera.transform.position;
}
}
This works Fine/Okay when I try it out, however as soon as I enter Climb() In my Climber script:
void Climb()
{
InputDevices.GetDeviceAtXRNode(climbingHand.controllerNode)
.TryGetFeatureValue(CommonUsages.deviceVelocity, out Vector3 velocity);
character.Move(transform.rotation * -velocity * Time.fixedDeltaTime);
cachedVelocity = -velocity;
Debug.Log(cachedVelocity);
}
When this is Climb() Runs, this happens:
Image that Shows The Issue
I don't see a reason for this to happen, maybe its very obvious. I don't know... Anyways, my question is: "How do I make the Collider of the CC Follow the Player Camera?".
In the CCCameraFollower.LateUpdate() method you are actually moving the center of the character controller and not the position of the Camera. Change to this and it will probably work as you want.
void LateUpdate()
{
Camera.transform.position = character.center;
}

Object following cursor freezes after camera pan operation begins

I have a GameObject, let's call it editor, that follows the position of the mouse in the world using the below script:
using UnityEngine;
public class FollowCursor : MonoBehaviour
{
void Update()
{
Plane objPlane = new Plane(-Camera.main.transform.forward,
gameObject.transform.position);
Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (objPlane.Raycast(mouseRay, out float distance))
{
gameObject.transform.position = mouseRay.GetPoint(distance);
}
}
}
This editor object is used in many places, one of which is to control panning of the camera. I control this panning using the script below, which is attached to the main camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraControls : MonoBehaviour
{
FollowCursor editor;
Vector2 camPosOnClick;
Vector2 mousePosOnClick;
public Vector2 RelativeToCamera(Vector2 worldPoint)
{
return worldPoint - (Vector2)gameObject.transform.position;
}
public Vector2 RelativeToWorld(Vector2 camPoint)
{
return camPoint + (Vector2)gameObject.transform.position;
}
void Start()
{
editor = GameObject.FindWithTag("GameController")
.GetComponent<FollowCursor>();
}
void Update()
{
if (Input.GetMouseButtonDown(1))
{
camPosOnClick = gameObject.transform.position;
mousePosOnClick =
RelativeToCamera(editor.gameObject.transform.position);
}
if (Input.GetMouseButton(1))
{
Vector2 change = mousePosOnClick
- RelativeToCamera(editor.gameObject.transform.position);
gameObject.transform.position = camPosOnClick + change;
}
}
}
The intended behavior of this code is upon right clicking, the position of the editor (tracking the mouse position) and the camera are saved. Each frame, the distance between the current editor position and the saved position (which equates to the drag distance) is added to the camera's saved position.
I can see no reason this code shouldn't work, but nothing happens when I right click. The action of right clicking causes the editor object to stop tracking its position; the raycast it uses fails every frame after right-clicking. I cannot figure out what right-clicking is doing that affects this raycast, nor how to fix it. Is there a flaw in this code that I have missed?

Why cant I move my camera in (Unity)C#?

camera transform
I am trying to move my camera based on the players' movements on Y axis in Unity.
However, it does not work...
What did I do wrong? I have attached image of my script (C#) here.
and, Yes, I did attach this script with Main Camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController : MonoBehaviour {
GameObject player;
// Use this for initialization
void Start () {
this.player = GameObject.Find("cat");
}
// Update is called once per frame
void Update () {
Vector3 playerPos = this.player.transform.position;
transform.position = new Vector3(
transform.position.x, playerPos.y, transform.position.z);
}
}
Make the player GameObject public and just drag and drop your player in the inspector in unity see if that works? Are you getting any exceptions? Also add Debug.Log (player.transform.position.ToString ()) to see if it is showing the right values. Are you sure you player object name is cat and not Cat, it is case sensitive. Check on those things and let me know if you figured it out!

Categories

Resources