Unity Roll-A-Ball Main Camera - c#

I can't get my camera's position to move with the player.
This is the CameraController.cs
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public GameObject Player;
private Vector3 offset;
void Start()
{
transform.position = Player.transform.position;
}
void LateUpdate()
{
transform.position = Player.transform.position;
Debug.LogError(transform.position);
}
}
The script is a component of the main camera. The camera is not a child of the player object or vice versa.
The debugging says that the position is being updated to the player's position, but when the game is run the camera is static and doesn't move from its initial starting point.

Try this:
using UnityEngine;
using System.Collections;
public class CameraController: MonoBehaviour {
public GameObject Player;
private Vector3 offset;
void Start () {
offset = transform.position - Player.transform.position;
}
void LateUpdate () {
transform.position = Player.transform.position + offset;
}
}
The offset is distance between camera and player.
Another way is by making camera a child of player.

using UnityEngine;
using System.Collections;
public class CameraFollower : MonoBehaviour
{
public Transform thePlayer;
private Vector3 offset;
void Start()
{
offset = transform.position - thePlayer.position;
}
void Update()
{
Vector3 cameraNewPosition = new Vector3(thePlayer.position.x + offset.x, offset.y, thePlayer.position.z + offset.z);
transform.position = cameraNewPosition;
}
}

Thanks so much for posting and trying to help. The problem was that I was trying to get the script to move the camera when it was in a Virtual Reality supported environment. I have found that the way the camera behaves, and subsequently how the camera is moved, is different when it is in a VR environment and the camera needs to be a child of an object to be moved and cannot be moved via scripts.

Related

Instantiate not showing object

I am trying to get a object to spawn at mouse position in unity 2d
whenever I click, but none of the objects are showing. It adds new clones in the hierarchy, but just not showing.
Here Is the script for the game controller object
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gamecon : MonoBehaviour
{
public GameObject square;
public void Start()
{
}
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 spawnpos = Camera.main.WorldToScreenPoint(Input.mousePosition);
GameObject g =Instantiate(square, (Vector2)spawnpos, Quaternion.identity);
}
}
}
I cant find any answers on the web that apply to my situation.
Thanks for help.
The solution is to use the ScreenToWorldPoint method and not the WorldToScreenPoint because what you need is the world position to spawn your object.
Use the following code:
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 spawnpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
GameObject g =Instantiate(square, (Vector2)spawnpos, Quaternion.identity);
}
}

How do I get my little spaceship to flip on it's Y-Axis?

Below is the code I'm using. Basically, it is a ball with the sprite of a tiny
spaceship that rotates around a ring. It has a trail renderer attached and
when I change the direction the trail comes out the top in the wrong direction.
I need to know how to flip the ship to match the trail.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallRotation : MonoBehaviour
{
[SerializeField] private float _speed;
private void FixedUpdate()
{
transform.Rotate(0, 0, _speed * Time.deltaTime);
}
public void ChangeDirection()
{
_speed = -_speed;
}
}
Hard to pinpoint ur error without knowing further details.
Is your "ball" a 3D Sphere or a 2D Circle?
What component is used to display your spaceship image?
if its the SpriteRenderer, you can get a reference to that and flip the sprite
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BallRotation : MonoBehaviour
{
[SerializeField] private float _speed;
SpriteRenderer m_SpriteRenderer;
void Start()
{
//Fetch the SpriteRenderer from the GameObject
m_SpriteRenderer = GetComponent<SpriteRenderer>();
}
private void FixedUpdate()
{
transform.Rotate(0, 0, _speed * Time.deltaTime);
}
public void ChangeDirection()
{
_speed = -_speed;
//flip sprite along the y axis
m_SpriteRenderer.flipY = !m_SpriteRenderer.flipY
}
}

All the guns in the game shoot at the same time when I only want the gun that I'm holding to shoot

My problem is that when I'm holding a gun and shoot it, all the other guns on the floor start shooting as well. How do I make it so that only the gun that I'm holding with the mouse can shoot?
You pick up the gun with the mouse left click, and you shoot with right click
My pickup code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUp : MonoBehaviour
{
bool Pressed = false;
void OnMouseDown()
{
Pressed = true;
GetComponent<Rigidbody2D>().isKinematic = true;
}
void OnMouseUp()
{
Pressed = false;
GetComponent<Rigidbody2D>().isKinematic = false;
}
void Update()
{
if(Pressed)
{
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = mousePos;
}
}
}
My gun code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour{
public Transform firePoint;
public float fireRate = 15f;
public GameObject bulletPrefab;
public Transform MuzzleFlashPrefab;
private float nextTimeToFire = 0f;
void Update() {
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
nextTimeToFire = Time.time + 1f/fireRate;
Shoot();
}
}
void Shoot ()
{
Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Transform clone = Instantiate (MuzzleFlashPrefab, firePoint.position, firePoint.rotation) as Transform;
clone.parent = firePoint;
float size = Random.Range (0.02f, 0.025f);
clone.localScale = new Vector3 (size, size, size);
Destroy (clone.gameObject, 0.056f);
}
}
and my bullet code
using System.Collections.Generic;
using UnityEngine;
public class bulet : MonoBehaviour
{
public float speed = 40f;
public Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb.velocity = transform.right * speed;
}
}
Set a flag in your Weapon, something called liked pickedUp, and toggle it when the weapon is picked up/dropped. Then in your Weapon's Update function, check if you should process Inputs for that weapon based on its picked up state. If its not picked up, there is no need to check if it should fire a bullet or not.

Building a camera transition for switching rooms

I'm trying to create a Zelda type camera that changes position after you reach a trigger point.
this is the code for the camera movement
public class CameraMovement : MonoBehaviour {
public Transform target;
public float smoothing;
public Vector2 maxPosition;
public Vector2 minPosition;
void LateUpdate()
{
if (transform.position != target.position)
{
Vector3 targetPosition = new Vector3(target.position.x, target.position.y, transform.position.z);
targetPosition.x = Mathf.Clamp(targetPosition.x, minPosition.x, maxPosition.x);
targetPosition.y = Mathf.Clamp(targetPosition.y, minPosition.y, maxPosition.y);
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing);
}
}
}
and this is the code for the room transition
public class RoomMove : MonoBehaviour
{
public Vector2 cameraChange;
public Vector3 playerChange;
private CameraMovement cam;
// Start is called before the first frame update
void Start()
{
cam = Camera.main.GetComponent<CameraMovement> ();
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
cam.minPosition += cameraChange;
cam.maxPosition += cameraChange;
other.transform.position += playerChange;
}
}
}
I've already tried to look up the answer in google and I found something similar but it did not work for me.
This is a little difficult due to the lack of details, info, and desire.
First make sure your setup is correct.
Does the object with RoomMove on it have a 2D collider marked trigger?
Does player have the "Player" tag on it?
Does the main camera have the movement script with the target variable set to the instance of the player? (Not a prefab, from the hierarchy)
Double check all of these first.
What you have here leads me to believe that all of your levels are side by side in one scene correct?
You could try removing the clamping and simply give each room an empty transform in the middle and when you hit the transition change the camera's target.

My Player wont stick to the platform and I can't seem to fin a solution

So I am trying to make the player a child to the moving platform which is being moved by looking for waypoints and going to them. But when I make the player collide with the platform the collision enter is not detecting the player thus not making the player stay on it, it instead glides off.enter image description here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movePlatform : MonoBehaviour
{
public GameObject[] waypoints;
float rotSpeed;
int current = 0;
public float speed;
float WPradius = 1;
private GameObject target = null;
private Vector3 offset;
public GameObject Player;
CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
//This will make the player a child of the Obstacle
controller.transform.parent = other.gameObject.transform;
}
}
void OnTriggerExit(Collider other)
{
controller.transform.parent = null;
}
// Update is called once per frame
void Update()
{
if (Vector3.Distance(waypoints[current].transform.position, transform.position) < WPradius)
{
current++;
if (current >= waypoints.Length)
{
current = 0;
}
}
transform.position = Vector3.MoveTowards(transform.position, waypoints[current].transform.position, Time.deltaTime * speed);
}
}
This will not make the player a child of the platform:
controller.transform.parent = other.gameObject.transform;
Because here controller is the CharacterController instance, and other refers to the player collider. So they both refers to the player transform in the end.
Replace it by something like
other.transform.parent = transform;
or
controller.transform.parent = transform;
(Here, transform refers to the platform transform.)
If the OnTriggerEnter() is not detecting, check if your collider has isTrigger enabled. Or change the method to OnCollisionEnter().
If you are making a 2D game, switch these methods to the 2D version (=> OnCollisionEnter2D or OnTriggerEnter2D).

Categories

Resources