I'm making a 2D platformer, and I want the camera to follow the character once it gets to the center, but follow it only on the x axis. I've downloaded this code, and it worked, but it followed the character on both the x and the y, and it stuck the character in the same corner of the camera that it started. I tried to add an if statement so that the camera only started moving once the offset was equal to the player, but it didn't work. I got the error code 'cannot implicitly convert vector3 to bool'
Here is the code:
using UnityEngine;
using System.Collections;
public class CompleteCameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
if (offset == player.transform.position)
{
transform.position = player.transform.position + offset;
}
}
}
The documentation seems to suggest that what you are doing is fine. But perhaps your code could be modified to fit this example? My gut says that this is going to give you the same error, but it could be worth trying.
https://docs.unity3d.com/ScriptReference/Vector3-operator_eq.html
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform other;
void Example()
{
if (other && transform.position == other.position)
{
print("I'm at the same place as the other transform!");
}
}
}
You could also try using the Vector3.Equals() method instead if you're ok with doing an exact comparison.
https://docs.unity3d.com/ScriptReference/Vector3.Equals.html
You could also try implementing your own function to compare the two vectors.
https://answers.unity.com/questions/395513/vector3-comparison-efficiency-and-float-precision.html
I really can't see anything wrong with the code you've posted, is it possible the error exists in another file?
Related
I was just copying a tutorial on YouTube on how to make the pong game and when it came to scripting the ball, there was an error for me but not the guy in the video.
I have no experience in C#, only Python. This is the error message:
Assets\Ball.cs(24,26): error CS0117: 'Random' does not contain a definition for 'range'
Anyone know what's wrong?
This is the script for it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float speed;
public Rigidbody2D rb;
//Start is called before the first frame update
void Start()
{
Launch();
}
//Update is called once per frame
void Update()
{
}
private void Launch()
{
float x = Random.range(0,2) == 0 ? -1 : 1;
float y = Random.range(0,2) == 0 ? -1 : 1;
rb.velocity = new Vector2(speed * x, speed * y);
}
}
You have to write Random.Range and not Random.range
You need to capitalize "range", Random.Range is a method and most methods start with a capital letter so instead of "Random.range", you need to type "Random.Range"
Here the script method just calls the "Launch()" method at initialization time, your "Lauch()" method is problematic, you can try to modify the "Random.range(0,2)" method to "Random.Range(0, 2)"
Im trying to make my character jump in unity2D,the jump itself works but i cant properly check if my player touches the ground.
I get 2 errors in the unity console.
the first one :
Physics2D does not contain a defenition for OverLapArea.
the second error :
Vector2 does not contain a constructor that takes 3 arguments.
(also i apologise for my bad english,its not my native language)
The script :
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class jump : MonoBehaviour
{
public bool IsGrounded;
public LayerMask platfomLayer;
public Rigidbody2D rb2D ;
// Start is called before the first frame update
void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
IsGrounded = Physics2D.OverLapArea (new Vector2(transform.position.x - 0.5f, transform.position.x - 0.5f),
new Vector2(transform.position.x + 0.5f, transform.position.y - 0, 51f),platfomLayer);
{
if (Input.GetKeyDown("space") && IsGrounded) rb2D.AddForce(transform.up * 2000f);
}
}
}
The errors are both correct.
OverLapArea is spelled correctly but capitalized wrong. Unity references are case-sensitive.
You are indeed trying to pass 3 values into a new Vector2. Look carefully at what you're passing into your second new Vector2 value. You have three values separated by commas within the parenthesis. It looks like you mis-typed "0.5f" as "0, 51f"
I look for the best way to detect, what site of an GO (cube) is facing upwards.
My research led me to the Dot-product.
I know what I want to do, but I guess my c# skills are too bad..
Basically I just want to find the Dot-product for example for the x-rotation.
And if its between a value of 0,9 to 1 one site is facing upwards, for -0,9 to -1 the other Site and so on for all axes.
In the Unity's documentation you have the following example. You just have to tweak it a little bit in order to achieve what you need.
// detects if other transform is behind this object
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
public Transform other;
void Update()
{
if (other)
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 toOther = other.position - transform.position;
if (Vector3.Dot(forward, toOther) < 0)
{
print("The other transform is behind me!");
}
}
}
}
I've recently started learning c# and unity and we're supposed to make a game where a ball rolls around a maze, is in danger of being destroyed by an enemy, and a message pops up when you reach the end. I have most of this done; however, my enemies, who are supposed to move back and forth and destroy the ball when touched, don't work. The walls and floor explodes out when the game is started and I'm not even sure they work at all. In our current assignment, we have to add classes and add another player (which I'm pretty sure I know how to do already). Here's my current code for my enemy class:
using UnityEngine;
using System;
[System.Serializable]
public class Boundary
{
public float xMin, xMax;
}
public class BadGuyMovement : MonoBehaviour
{
public Transform transformx;
private Vector3 xAxis;
private float secondsForOneLength = 1f;
public Boundary boundary;
void Start()
{
xAxis = Boundary;
}
void Update()
{
transform.position = new Vector3(Mathf.PingPong(Time.time, 3), xAxis);
}
void OnTriggerEnter(Collider Player)
{
Destroy(Player.gameObject);
}
}
On lines 21 (xAxis = Boundary;) and 26 (transform.position = new Vector 3) there are errors that I just completely don't understand. If you guys know how to fix it or know of a better way to do something, or at least a better way to move an object back and forth, please let me know!
Thank you so much for taking the time to answer this!
You're getting errors for two reasons, but fairly trivial.
The first error, on line 21 (xAxis = Boundary), you're getting an error because you're assigning a value of type Boundary to a variable of type Vector3.
This is like trying to say an Apple equals an Orange (they don't).
In languages like C#, the types of both the left hand and the right hand of an assignment must be the same.
Simply put
type variable = value; --> Works only if type from the LHS = type from the RHS
Your second error is happening is because you're trying to create a Vector3 with the wrong set of values. The process of creating a type is done through calling the Constructor of the class you're trying to create.
Now, look at the Constructor for a Vector3. One constructor takes 3 parameters of type float and the other takes 2 parameters, again of type float.
You're trying to call the constructor of a Vector3 using a float (from Mathf.PingPong) and a Vector3 (xAxis).
Now that we have that out of the way, try this code.
using UnityEngine;
using System;
[System.Serializable]
public class Boundary
{
public float xMin, xMax;
}
public class BadGuyMovement : MonoBehaviour
{
public Transform transformx;
private Vector3 xAxis;
private float secondsForOneLength = 1f;
public Boundary boundary; //Assuming that you're assigning this value in the inspector
void Start()
{
//xAxis = Boundary; //This won't work. Apples != Oranges.
}
void Update()
{
//transform.position = new Vector3(Mathf.PingPong(Time.time, 3), xAxis); //Won't work, incorrect constructor for a Vector3
//The correct constructor will take either 2 float (x & y), or 3 floats (x, y & z). Let's ping pong the guy along the X-axis, i.e. change the value of X, and keep the values of Y & Z constant.
transform.position = new Vector3( Mathf.PingPong(boundary.xMin, boundary.xMax), transform.position.y, transform.position.z );
}
void OnTriggerEnter(Collider Player)
{
Destroy(Player.gameObject);
}
}
I am new to A* Pathfinding i want to get the enemy ai to move only in x axis. How can i do that? this is what i got right now:
using UnityEngine;
using System.Collections;
using Pathfinding;
public class SimpleZombieAI : MonoBehaviour {
public Vector2 targetPosition;
public void Start (){
Seeker seeker = GetComponent<Seeker> ();
seeker.StartPath (transform.position, targetPosition, OnPathComplete);
}
public void OnPathComplete (Path p) {
Debug.Log ("Yay, we got a path back. Did it have an error? "+p.error);
}
}
but it goes in x and y and i want to make it go ONLY x. I hope someone can help me because i dont know where to post about this question.
I'm assuming your Seeker.StartPath function takes Vector parameters, so just passing the x values won't work. While I don't think you would really need to use pathfinding for a one-dimensional path, if that's what you want to use, here's a way that you might be able to achieve that.
public void Start (){
Seeker seeker = GetComponent<Seeker> ();
float myY = transform.position.y;
seeker.StartPath (transform.position, new Vector2(targetPosition.x,myY), OnPathComplete);
}
This will have your enemies move along their constant y-value line, while chasing from their own x-coordinate to the player's x-coordinate.