Question by
SelimEminS · Sep 17, 2020 at 03:24 PM ·
c#androidcrossplatform
Hit camera border for kinematic rigidbody
hi! i made a android game and main character has kinematic rigidbody (im moving it left or right with CrossPlatformInput). i tried lots of ways to make the character hit camera border and all didnt work except Alexander Zotov's code. code is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StayInside : MonoBehaviour {
// Update is called once per frame
void Update () {
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -2f, 2f),
Mathf.Clamp(transform.position.y, -4f, 4f), transform.position.z);
}
}
this code working but it does not support all devices. i tried modify this code and i made this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class stayInBorder : MonoBehaviour
{
void Update()
{
Camera cam = Camera.main;
float height = 2f * cam.orthographicSize;
float width = height * cam.aspect;
transform.position = new Vector3(Mathf.Clamp(transform.position.x, width, height),
Mathf.Clamp(transform.position.y, width, height), transform.position.z);
}
}
this didnt work. so can you help me?
Comment
Your answer
Follow this Question
Related Questions
Screenshot on Android 2 Answers
user can't log into google play services 1 Answer
Determine Wifi adapter address on Android 0 Answers
What's wrong with my audiostream on android? 0 Answers
anyone want to make a game? 1 Answer