im new to unity game development. i have to develop simple 2D object movement in square path during mouse button clicked. just simple square/circle 2D spirite move in square path during mouse clicked
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices.ComTypes;
using UnityEngine;
public class mousetomove : MonoBehaviour
{
public float speed = 5.0f;
private Transform target1;
private Transform target2;
private Transform hero ;
// Start is called before the first frame update
void Start()
{
hero = GameObject.FindGameObjectWithTag("Hero").GetComponent<Transform>();
target1 = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
if (hero == target1)
{
transform.position = Vector2.MoveTowards(transform.position, target2.position ,
*Time.deltaTime);
}
}
}
}
Here you are trying to check if hero is equal to target, this will not happen I think, you have to compare positions but even doing so positions will probably not be the same but just very close. You are using MoveTowards, thats good but you have to be changing the target, like maybe have 4 targets in an array I guess
Related
I'm currently trying to make a camera go to an object when right click is held however it's not working how do I fix this? When the button right click is pressed it prints hi and hello however the camera does not move.
Edit: The line "transform.localPosition = MagicWandReg1Pos;" and " transform.localPosition = CameraReg1Pos;" is what i think is the problem but i just don't know how to fix it.
Edit 2: the problem is the addon cinemachine (i think ) I'm currently working on fixing it).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camscript : MonoBehaviour
{
public Vector3 MagicWandReg1Pos;
public Vector3 CameraReg1Pos;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
MagicWandReg1Pos = new Vector3(0.9525713f,2,2400557f);
CameraReg1Pos = new Vector3(-5.601483f,3,150208);
transform.localPosition = CameraReg1Pos;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
transform.localPosition = MagicWandReg1Pos;
print("Hello");
}
if (Input.GetKeyUp(KeyCode.Mouse1))
{
transform.localPosition = CameraReg1Pos;
print("Hi");
}
}
}
I am a super newbie in Unity and am learning by making my first game. I want the camera to follow the player, but only in the X axis. I had earlier made the camera a child of the player, but that didn't work as I wanted. So I whipped up a C# script to follow the player, as given below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cameraFollow : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.position = new Vector3(GameObject.Find("robot_body").transform.position.x, 0f, 0f);
}
}
However, this is showing only the blue background when run. Am I doing something wrong?
By setting your z position to 0 your camera probably ends up to close to everything in order to render it.
Try to not overwrite y and z with 0 but rather keep the current values:
// If possible rather already reference this via the Inspector!
[SerializeField] private GameObject robot;
private void Start()
{
// As fallback get it only ONCE
if(!robot) robot = GameObject.Find("robot_body");
}
void Update()
{
// get the current position
var position = transform.position;
// overwrite only the X component
position.x = robot.transform.position.x;
// assign the new position back
transform.position = position;
}
As you can see in the screenshoot I can't see prefabs in the game tab but only in the editor. I have made a simple function for shooting(not finished yet), it works fine, it spawns the prefabs but i can't see them in the game tab, I have already tried changing the Sorting Layer, move the camera, change Z position but nothing appen.
This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAttack : MonoBehaviour
{
[SerializeField]
float delayBetweenShots = 0.4f;
float timePassedSinceLast = 0f;
// Start is called before the first frame update
void Start()
{
timePassedSinceLast = delayBetweenShots;
}
// Update is called once per frame
void Update()
{
Aiming();
Shooting();
}
void Aiming()
{
var objectPos = Camera.main.WorldToScreenPoint(transform.position);
var dir = Input.mousePosition - objectPos;
transform.rotation = Quaternion.Euler(new Vector3(0,0,Mathf.Atan2(-dir.x, dir.y) * Mathf.Rad2Deg));
}
void Shooting()
{
if(Input.GetMouseButton(0) && timePassedSinceLast >= delayBetweenShots)
{
GameObject bullet = (GameObject)Instantiate(Resources.Load("bullet"), transform.position, transform.rotation);
timePassedSinceLast = 0f;
}
else
{
timePassedSinceLast += Time.deltaTime;
}
}
}
The prefabs get instantiated correctly. As others suggested as well, the best way to find "lost" objects in your game is to shoot some stuff, pause the game, go into scene view, turn on 3D mode and double click one of the prefabs in the hierarchy. The camera will take you straight to your object.
This is a reduced version of the player controller I am using now, which still produces the error explained below:
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
public class FirstPersonController : MonoBehaviour
{
[SerializeField] private float m_RunSpeed;
private CharacterController m_CharacterController;
// Use this for initialization
private void Start()
{
m_CharacterController = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
Vector3 move = Vector3.right * m_RunSpeed;
m_CharacterController.Move(move * Time.fixedDeltaTime);
}
}
}
I am using the standard assets script and I am trying to have a script teleport the player to different places. When I try and move the player it goes to that position for a frame, then goes right back to its position.
player.transform = new Vector3(1,2,3);
// works as expected, but then next frame, player's
// position is back to where it was before
This problem occurs because Move may not be able to read the up-to-date values for transform.position.
This problem has been reported on the official Unity Issue Tracker here, and a solution was posted there as well:
The problem here is that auto sync transforms is disabled in the physics settings, so characterController.Move() won't necessarily be aware of the new pose as set by the transform unless a FixedUpdate or Physics.Simulate() called happened in-between transform.position and CC.Move().
To fix that, either enable auto sync transforms in the physics settings, or sync manually via Physics.SyncTransforms right before calling Move()
So, you could fix your problem by editing FixedUpdate so that it calls Physics.SyncTransforms before Move, like this:
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof (CharacterController))]
public class FirstPersonController : MonoBehaviour
{
[SerializeField] private float m_RunSpeed;
private CharacterController m_CharacterController;
// Use this for initialization
private void Start()
{
m_CharacterController = GetComponent<CharacterController>();
}
private void FixedUpdate()
{
Vector3 move = Vector3.right * m_RunSpeed;
Physics.SyncTransforms();
m_CharacterController.Move(move * Time.fixedDeltaTime);
}
}
}
I am really new to unity so I wanted to make a simple 2d project where you can move a cube. So I made a script to move the cube but when I play the game the Z changes along with the X so it will fall of the map.
Video:
https://www.youtube.com/watch?v=M9oHSc6dN2A&feature=youtu.be
The script I'm using:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
private Vector2 input;
public float movementSpeed = 50f;
private float horizontal;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
horizontal = Input.GetAxis ("Horizontal");
rigidbody.AddForce ((Vector2.right * movementSpeed) * horizontal);
}
}
I am using unity 4
Your rigidbody has Use Gravity checked. Romove that and it should function the way you want. [Wrong axis]
Edit:
A Rigidbody has a constraint property. Freeze z position there.