I am a bit confused as to why this is, so basically Im trying to get it to where the camera follows the player without moving left and right with him.
using System.Collections;
using UnityEngine;
public class CameraMotor : MonoBehaviour {
private Transform lookAt;
private Vector3 startOffset;
private Vector3 moveVector;
void Start () {
GameObject.FindGameObjectWithTag ("Player").transform;
}
// Update is called once per frame
void Update () {
moveVector = lookAt.position + startOffset;
//X
moveVector.x = 0; //center of track
//Y[image][1]
moveVector.y = Mathf.Clamp(moveVector.y,3,5);// for ramps/stairs
transform.position = moveVector;
}
}
I think you want GameObject.FindWithTag
https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html
Even so,
GameObject.FindWithTag ("Player").transform;
does nothing. (Nothing usefull atleast)
lookAt is never assigned, so im guessing what you want to do is
lookAt = GameObject.FindWithTag ("Player").transform;
If you are looking for an object in your scene, then use this simple script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnswerScript : MonoBehaviour {
private Transform lookAt;
// Use this for initialization
void Start () {
lookAt.Find("The Object That You are Looking For");
}
// Update is called once per frame
void Update () {
}
}
Related
I am making my object follow the rotation of another object. I want my object to rotate with very little difference, that is, from its current rotation it should not rotate completely with the other object.
There should be a difference u pto its rotation such that it should rotate 10% of what the main object rotates. How do I do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowRotationWithLimit : MonoBehaviour {
public GameObject objectToFollow;
// Start is called before the first frame update
void Start () {
}
// Update is called once per frame
void Update () {
this.transform.eulerAngles = new Vector3 (this.transform.eulerAngles.x, objectToFollow.transform.eulerAngles.y, this.transform.eulerAngles.z);
}
}
I am not able to get that logic of creating that difference.
I would rather call it FollowRotationWithFactor then and simply use a multiplier
public class FollowRotationWithFactor : MonoBehaviour
{
public GameObject objectToFollow;
public float factor = 0.1f;
// Update is called once per frame
void Update ()
{
var eulerAngles = transform.eulerAngels;
eulerAngles.y = objectToFollow.transform.eulerAngles.y * factor;
transform.eulerAngles = eulerAngles;
}
}
I am trying to teleport the character when they touch a cube, but for some reason, it prints the message that it the collision is being detected, but the character's position is not changing. It works for another scenario that is almost exactly the same, but even if I put this in another script, it does not teleport the character. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class othercollision : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.name == "teleportcube")
{
gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
Debug.Log("Collision detected");
}
}
}
Here is the script that has something similar working:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class detectcollision : MonoBehaviour
{
public GameObject tplocation1;
public GameObject player;
public GameObject entranceloc;
void OnControllerColliderHit(ControllerColliderHit hit)
{
//This if statement does Debug.Log the message and it also changes the player's position.
if (hit.gameObject.name == "trigger")
{
Debug.Log("triggered");
gameObject.transform.position = tplocation1.transform.position;
Thread.Sleep(7);
Application.Quit();
}
//This if statement does not change the player's position, but Debug.Logs the teleporting message.
if (hit.gameObject.name == "entrance")
{
gameObject.transform.position = new Vector3(-3184.53f, 20.35f, -171.585f);
Debug.Log("teleporting...");
}
}
}
Try to make the Vector3 beforehand as a variable or a temporary variable and then assign it to the position. If you did then maybe the values that you have entered are out of boundary somewhy, but that's unlikely, try changing them.
I have a script that makes the camera do a shake by putting a button because
It is a public access function, if I do it that way when placing a button it works well but what I cannot achieve is to call the function so that every time my player collides with an enemy he makes the shake. I hope you can help me.
The shake code in camera is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScreenShaker : MonoBehaviour {
private float shakeAmount = 0.5f;
private float shakeTime = 0.0f;
private Vector3 initialPosition;
private bool isScreenShaking = false;
void Update () {
if(shakeTime > 0)
{
this.transform.position = Random.insideUnitSphere * shakeAmount + initialPosition;
shakeTime -= Time.deltaTime;
}
else if(isScreenShaking)
{
isScreenShaking = false;
shakeTime = 0.0f;
this.transform.position = initialPosition;
}
}
public void ScreenShakeForTime(float time)
{
initialPosition = this.transform.position;
shakeTime = time;
isScreenShaking = true;
}
}
The enemy code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControladorEnemigoCirculoMediano : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Here I don't know what to call the public void function ScreenShakeForTime (float time);
I already tried many things online but when my character comes into contact with the character, I don't do the shake in the camera.
}
}
}
You can create Unity-singleton in your ScreenShaker class like:
class ScreenShaker
{
public static ScreenShaker Instance {private set; get;};
void Awake() {
Instance = this;
}
}
And than from any place to call like:
ScreenShaker.Instance.ScreenShakeForTime(2f);
This is the easiest way, but maybe it's better to create standard singeleton(it's up to you).
And also don;t forget to destroy it on OnDestroy()
can you tell me in enemy game object collider isTrigger is enable or not
if it is not enable then use OnColliderEnter2D(Collision2D other){} for collision detection
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
Hello I am new to coding in unity and i was trying to make the health bar go down when the player hits an object. When i use a debug.log it prints what i want it to print when it collides with the object however when i try to make the health go down when it hits the object it gives me this error
NullReferenceException:
Object reference not set to an instance of an object
DamagePlayer.OnCollisionEnter2D (UnityEngine.Collision2D collision)
(at Assets/Scripts/DamagePlayer.cs:30)
here is my code.
My Damage class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamagePlayer : MonoBehaviour
{
public BarScript bar;
public int playerHealth = 100;
int damage = 10;
// Start is called before the first frame update
void Start()
{
print(playerHealth);
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.collider.tag =="enemy")
{
Debug.Log("enemy");
bar.math(damage);
}
}
}
Health Bar class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BarScript : MonoBehaviour
{
private float fillAmount;
[SerializeField]
private Image content;
// Start is called before the first frame update
void Start()
{
fillAmount = 1f;
}
// Update is called once per frame
void Update()
{
content.fillAmount = fillAmount;
}
public float math(float value)
{
return fillAmount =(value / 100);
}
}
My Player class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D rb;
[SerializeField]
private float speed = 300f;
private float jump = 400f;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Movement();
}
void Movement()
{
if (Input.GetKeyDown("d")){
rb.velocity = new Vector2(speed * Time.fixedDeltaTime, rb.velocity.y);
}else if (Input.GetKeyDown("a"))
{
rb.velocity = new Vector2(-speed * Time.fixedDeltaTime, rb.velocity.y);
}else if (Input.GetKeyDown("space"))
{
rb.velocity = new Vector2(rb.velocity.x, jump * Time.fixedDeltaTime);
}
}
}
You haven't create an instance of BarScript in DamagePlayer, that's why this is creating problem.
Without instance you can't access member method of a class, you can use static to make that accessible by anyone. If it is just for Player then you can do that (Single-player), But it won't be so nice.
If you it's multi-player or you want do the same for enemy, then create prefab with both script, then instantiate or use pooling. Finally use findGameobjectsWithTag (not findGameobjectWithTag) and access those scripts, members and use their member methods.
I just want to move a bullet (but it doesn't work) and test on cube (but the code too did not move the cube).
The commented codes also did not move the game object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movetest : MonoBehaviour
{
//public GameObject cube;
// Use this for initialization
public GameObject testmovecube;
public Rigidbody rb;
void Start () {
//testmovecube = this.GetComponent<GameObject>();
//rb = testmovecube.GetComponent<Rigidbody>();
move();
//
}
// Update is called once per frame
void Update () {
}
private void move()
{
testmovecube.transform.position =
Vector3.MoveTowards(transform.position, new Vector3(226, 1, 226) , 2);
//transform.Translate(Vector3.forward);
//rb.AddForce(Vector3.forward);
}
}
Please any help is appreciated.