ImageCamera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainGame : MonoBehaviour
{
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
}
}
}
I made a 3D game and I want my camera to stay in the a fix position and just rotate, this is the script that I used. What do I need to add to fix the camera and just rotate?
Related
I am fairly new to C# and unity and I am trying to make a square jump with a Vector2 but for some reason the square wont move. Here is the code for you to see:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float Jump;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Jump = Input.GetAxis("Jump");
if (Jump == 1)
{
new Vector2(0, 1);
}
}
}
try this
//player must have a rigidbody2D and a box colider
public float moveSpeed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * moveSpeed;
}
void Jump()
{
if (Input.GetButtonDown("Jump"))
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}
My collision detection is not working! Debug message wont pop up when I collide with a game object with the tag "Enemy". Does anyone know what the issue is? Please help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using UnityEngine.TextMeshPro;
using TMPro;
public class PlayerController : MonoBehaviour
{
public float speed = 3;
public float jumpPower = 1.5f;
private Rigidbody2D rigidBody;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.A))
transform.Translate(-.1f, 0.0f, 0.0f);
if(Input.GetKey(KeyCode.D))
transform.Translate(.1f, 0.0f, 0.0f);
}
**void OnCollisionEnter(Collision col){
if(col.gameObject.CompareTag("Enemy")){
Debug.Log("CONTACT");**
}
}
}
Not sure if this already solves your issue but:
The issue might be caused by you moving an Object with a Rigidbody2D component by using the transform component. This breakes the physics and collisions might not get called.
Rather always go through the Rigidbody2D component and use Rigidbody2D.MovePosition in FixedUpdate like
bool keyA;
bool keyD;
void Update()
{
if(Input.GetKey(KeyCode.A)) keyA = true;
else if(Input.GetKey(KeyCode.D)) keyD = true;
}
private void FixedUpdate ()
{
if(keyA)
{
keyA = false;
rigodBody.MovePosition(rigiBody.position - Vector3.right * 0.1f);
}
else if(keyD)
{
keyD = false;
rigidBody.MovePosition(rigidBody.position + Vector2.right * 0.1f);
}
}
In general your Rigidbody should be isKinematic in order to work
Note: MovePosition is intended for use with kinematic rigidbodies.
I make a 3d game.My gun only shoot the ground , even when I select other object to shoot. I have the ground at a rotation , it may affects the game? Should I add something to the main camera?
using UnityEngine;
public class Gun : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public Camera fpsCam;
// Start is called before the first frame update
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if( Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
}
}
}
picture ground
I am making a 2D platfomer and I am using screen space-camera render mode in canvas. Now the background fits inside the screen in every aspect ratio perfectly. But when I make the camera follow the character, the background also comes with it, making the character look like it is not moving.
Code for player movement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
private Rigidbody2D myRigidbody;
[SerializeField]
private float movementSpeed;
// Use this for initialization
void Start ()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate ()
{
float horizontal = Input.GetAxis ("Horizontal");
HandleMovement(horizontal);
}
private void HandleMovement(float horizontal)
{
myRigidbody.velocity = new Vector2 (horizontal * movementSpeed, myRigidbody.velocity.y);
}
}
Here is the camera follow code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
Vector3 velocity = Vector3.zero;
public float smoothTime = 0.15f;
public bool YMaxEnabled = false;
public float YMaxValue = 0;
public bool YMinEnabled = false;
public float YMinValue = 0;
public bool XMaxEnabled = false;
public float XMaxValue = 0;
public bool XMinEnabled = false;
public float XMinValue = 0;
void FixedUpdate()
{
Vector3 targetPos = target.position;
//vertical
if (YMinEnabled && YMaxEnabled)
{
targetPos.y = Mathf.Clamp (target.position.y, YMinValue, YMaxValue);
}
else if (YMinEnabled)
{
targetPos.y = Mathf.Clamp (target.position.y, YMinValue, target.position.y);
}
else if (YMaxEnabled)
{
targetPos.y = Mathf.Clamp (target.position.y, target.position.y, YMaxValue);
}
//horizontal
if (XMinEnabled && XMaxEnabled)
{
targetPos.x = Mathf.Clamp (target.position.x, XMinValue, XMaxValue);
}
else if (YMinEnabled)
{
targetPos.x = Mathf.Clamp (target.position.x, XMinValue, target.position.x);
}
else if (YMaxEnabled)
{
targetPos.x = Mathf.Clamp (target.position.x, target.position.x, XMaxValue);
}
targetPos.z = transform.position.z;
transform.position = Vector3.SmoothDamp (transform.position, targetPos, ref velocity, smoothTime);
}
}
If you use screen space camera, then the Canvas will move with the camera. I would suggest using Sprite Renderer instead of Canvas panel for level background. If you need to scale the Sprite according to screen, do it from the code. Also, for scrolling the background, you can follow this tutorial:
https://unity3d.com/learn/tutorials/topics/2d-game-creation/2d-scrolling-backgrounds
Hi I am working with vuforia sdk in Unity..I have an AR Object.How to rotate the object 360 Degrees(rotate all sides) by touch? .So far i can rotate it left and right using following code..
Thanks!!
using UnityEngine;
using System.Collections;
public class Rotate : MonoBehaviour {
public Bounds bounds;
public float speed = 1.0F;
// Use this for initialization
void Start () {
var collider = gameObject.GetComponent<BoxCollider> ();
if (collider == null) {
collider = gameObject.AddComponent<BoxCollider>();
Debug.Log("No Collider is Detected");
}
bounds = collider.bounds;
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0)){
if(bounds.size.magnitude > 0){
var dtx = Input.GetAxis("Mouse X")*speed;
var dty = Input.GetAxis("Mouse Y")*speed;
var pivot = bounds.center;
transform.RotateAround(pivot,Vector3.up,dtx);
transform.RotateAround(pivot,Vector3.left,dty);
transform.Rotate(Vector3.up * Time.deltaTime * speed);
}
}
}
}